使用Java读取JSON数据

----------------siwuxie095

   

   

   

   

   

   

JSON 官网:http://www.json.org/

   

在官网页面的下方,是 JSON 数据格式在各个语言中的实现方法和操作类库

   

找到 Java 语言,选择 google-gson (成熟,且操作简单,被大多数开发者所使用的)

   

   

google-gson 下载链接:https://github.com/google/gson

   

截止 2017/3/30 最新版本 gson-2.8.0 下载链接:

http://download.csdn.net/detail/siwuxie095/9799544

   

   

   

   

工程名:TestReadJSON

包名:com.siwuxie095.json

类名:ReadJSON.java

   

   

打开资源管理器,在工程 TestReadJSON 文件夹下,放入

一个 JSON 文件:test.json

   

test.json 的内容:

   

   

   

   

打开资源管理器,在工程 TestReadJSON 文件夹下,创建一个

文件夹:lib,在其中放入:gson-2.8.0.jar

   

   

工程结构目录如下:

   

   

   

   

选择 gson-2.8.0.jar,右键->Build Path->Add to Build Path

   

此时,工程结构目录一览:

   

   

   

   

   

代码:

   

package com.siwuxie095.json;

   

import java.io.FileNotFoundException;

import java.io.FileReader;

   

import com.google.gson.JsonArray;

import com.google.gson.JsonIOException;

import com.google.gson.JsonObject;

import com.google.gson.JsonParser;

import com.google.gson.JsonSyntaxException;

   

public class ReadJSON {

 

/**

* 通过解析器将test.json转换成JsonObject

*

* @param args

*/

   

public static void main(String[] args) {

 

// test.json 的数据转换成 JSON 对象

//需要创建一个解析器,可以用来解析字符串或输入流

JsonParser parser=new JsonParser();

 

 

try {

 

//创建一个JSON对象,接收parser解析后的返回值

//使用parse()方法,传入一个Reader对象,返回值是JsonElement类型

//因为要读取文件,所以传入一个FileReader

//JsonObjectJsonElement的子类,所以需要强转

//有异常抛出,使用 try catch 捕获

JsonObject object=(JsonObject) parser.parse(new FileReader("test.json"));

 

 

//先将两个外部的属性输出 category pop

// get 到名称(键),返回的是 JsonElement,再 getAs 转换成什么类型的值

//依据 json 格式里的数据类型

System.out.println("category="+object.get("category").getAsString());

System.out.println("pop="+object.get("pop").getAsBoolean());

 

 

//接着读取test.json里的JSON数组,名称是languages(键)

//创建一个JsonArray

JsonArray array=object.get("languages").getAsJsonArray();

for (int i = 0; i < array.size(); i++) {

//分隔线

System.out.println("-----------------");

//创建一个JsonObject,从array的下标获取,get() 返回JsonElement类型

//这里不用强转,而用 getAsJsonObject() 进行转换

JsonObject subObject=array.get(i).getAsJsonObject();

System.out.println("id="+subObject.get("id").getAsInt());

System.out.println("name="+subObject.get("name").getAsString());

System.out.println("ide="+subObject.get("ide").getAsString());

 

}

 

 

 

 

} catch (JsonIOException e) {

e.printStackTrace();

} catch (JsonSyntaxException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

   

}

   

   

运行一览:

   

   

   

   

   

   

   

   

【made by siwuxie095】

原文地址:https://www.cnblogs.com/siwuxie095/p/6649298.html