Java创建和解析Json数据方法——org.json包的使用(转)
org.json包的使用
view plain copy
- package orgjson;
-
- /**
- * 包含getter和setter的java bean类
- * @author Zen9
- */
- public class Student {
- private String name;
- private String sex;
- private int age;
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
然后测试构造方法;也可以用put方法来向JSON对象中添加key/value对,当用put方法时候,value值可以是int、double、String、、boolean、collection、Map等等,但不可以为bean类型,代码如下:
[java] view plain copy
- package orgjson;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.json.*;
- public class JsonTest {
- public static void constructorTest() {
-
- String jsonStr = "{'name':'JTZen9','age':21}";
- JSONObject strJson = new JSONObject(jsonStr); // 传入字符串
- System.out.println("构造参数为String类:" + strJson);
-
- Map
map = new HashMap();
- map.put("age", 21);
- map.put("sex", "male");
- map.put("name", "JTZen9");
- JSONObject mapJson = new JSONObject(map); // 传入Map类型
- System.out.println("构造参数为Map类:" + mapJson);
-
- Student student = new Student();
- student.setAge(21);
- student.setName("JTZen9");
- student.setSex("male");
- JSONObject beanJson = new JSONObject(student); // 传入Bean类型
- System.out.println("构造参数为Bean类:" + beanJson);
- }
-
- public static void putMethodTest() {
-
- JSONObject jsonObject1 = new JSONObject();
- jsonObject1.put("bookName", "JTZen9");
- jsonObject1.put("age", 21);
- System.out.println(jsonObject1);
-
- JSONObject jsonObject2 = new JSONObject();
- List
- for (int i = 1; i < 3; i++) {
- Map
""> map = new HashMap"">();
- map.put("title", "java程序设计 第" + i + "版");
- map.put("price", i*20);
- list.add(map);
- }
- jsonObject2.put("book", list);
- System.out.println(jsonObject2);
-
- Student student = new Student();
- student.setAge(21);
- student.setName("JTZen9");
- student.setSex("male");
- jsonObject2 = new JSONObject(student);
- JSONObject jsonObject3 = new JSONObject();
- jsonObject3.put("people", jsonObject2); //不可以直接传bean类对象put("people",student)
- System.out.println(jsonObject3);
- }
-
- public static void main(String[] args) throws Exception {
- constructorTest();
- System.out.println("---------------------------------------------------------");
- putMethodTest();
- }
- }
输出结果:
view plain copy
- package orgjson;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.json.*;
- public class JsonArrayTest {
- public static void constructorTest() {
-
- String jsonStr = "[{'name':'JTZen9','age':21}]";
- JSONArray strJson = new JSONArray(jsonStr); // 传入字符串
- System.out.println("构造参数为String类:" + strJson);
-
- List
- for (int i = 1; i < 3; i++) {
- Map
""> map = new HashMap"">();
- map.put("title", "java程序设计 第" + i + "版");
- map.put("price", i*20);
- list.add(map);
- }
- JSONArray mapJson = new JSONArray(list); // 传入Collection类型
- System.out.println("构造参数为Collection类:" + mapJson);
-
- int[] numlist = new int[10];
- for (int i = 0; i < numlist.length; i++) {
- numlist[i] = i;
- }
- JSONArray arrayJson = new JSONArray(numlist); // 传入Array类型,实例1
- System.out.println(arrayJson);
-
- Student[] student = {new Student(),new Student()};
- student[0].setAge(21);
- student[0].setName("JTZen9");
- student[0].setSex("male");
- student[1].setAge(21);
- student[1].setName("heiheihei");
- student[1].setSex("female");
- JSONArray beanJson = new JSONArray(student); // 传入Array类型,实例2
- System.out.println("构造参数为Array类:" + beanJson);
- }
-
- public static void main(String[] args) {
- constructorTest();
- }
- }
输出结果如下:
view plain copy
- package orgjson;
- import java.util.HashMap;
- import java.util.Map;
- import org.json.*;
- public class JSONArrayTest {
- public static void putMethodTest() {
- JSONArray jsonArray1 = new JSONArray();
- jsonArray1.put("JTZen9");
- jsonArray1.put(21);
- jsonArray1.put("male");
- System.out.println(jsonArray1);
-
- JSONArray jsonArray2 = new JSONArray();
- Map
""> map = new HashMap"">();
- map.put("title", "java程序设计 第2版");
- map.put("price", 20);
- jsonArray2.put(map); //传入一个map
- System.out.println("传入一个Map:" + jsonArray2);
- map.clear();
- map.put("title", "java程序设计 第3版");
- map.put("price", 30);
- jsonArray2.put(map); //没有下标的直接在结果后面添加
- System.out.println("没有指定下标:" + jsonArray2);
- map.clear();
- map.put("title", "java程序设计 第1版");
- map.put("price", 10);
- jsonArray2.put(0,map); //使用下标可以添加到自定义的位置
- System.out.println("添加到第一个位置:" + jsonArray2);
-
- Student[] student = { new Student(), new Student() };
- student[0].setAge(21);
- student[0].setName("JTZen9");
- student[0].setSex("male");
- student[1].setAge(21);
- student[1].setName("heiheihei");
- student[1].setSex("female");
- JSONArray jsonArray3 = new JSONArray();
- jsonArray3.put(student);
- System.out.println("注意输出结果:" + jsonArray3);
-
- }
-
- public static void main(String[] args) {
- putMethodTest();
- }
- }
输出的结果如下:
view plain copy
- package orgjson;
- import java.io.PrintWriter;
- import java.util.HashMap;
- import java.util.Map;
- import org.json.*;
- public class JSONWriterStringerTest {
- public static void JSONStringerTest() throws Exception {
-
- PrintWriter writer = new PrintWriter("test.txt");
- JSONWriter jsonWriter = new JSONWriter(writer);
- jsonWriter.object().key("name").value("JTZen9").key("age").value(21).key("sex").value("male").endObject();
- writer.flush();
- writer.close();
-
- Map
""> map1 = new HashMap"">();
- map1.put("age", 21);
- map1.put("sex", "male");
- map1.put("name", "jtzen9");
- Map
""> map2 = new HashMap"">();
- map2.put("age", 21);
- map2.put("sex", "female");
- map2.put("name", "heiheihei");
- JSONStringer jsonStringer = new JSONStringer();
- jsonStringer.array().value(map1).value(map2).endArray();
- System.out.println(jsonStringer);
- }
-
- public static void main(String[] args) throws Exception {
- JSONStringerTest();
- }
- }
输出结果,上面为test.txt文件,下面为控制台:
view plain copy
- package orgjson;
- import java.io.File;
- import java.io.FileReader;
- import org.json.*;
- public class JSONTokenerTest {
-
- public static void readJsonFile() throws Exception{
- JSONTokener jsonTokener = new JSONTokener(new FileReader(new File("test.txt")));
- JSONObject jsonObject = new JSONObject(jsonTokener);
- System.out.println(jsonObject);
- System.out.println("姓名:" + jsonObject.getString("name"));
- System.out.println("年龄:" + jsonObject.getInt("age"));
- }
-
- public static void main(String[] args) throws Exception {
- readJsonFile();
- }
- }
输出结果如下:
view plain copy
- [
- {
- "institute":{
- "name":"Software Institute",
- "grade":[
- {
- "name":"freshman",
- "class":[
- {
- "no.":1,
- "students":61
- },
- {
- "no.":2,
- "students":62
- },
- {
- "no.":3,
- "students":63
- }
- ]
- },
- {
- "name":"sophomore",
- "class":[
- {
- "no.":1,
- "students":51
- },
- {
- "no.":2,
- "students":52
- },
- {
- "no.":3,
- "students":53
- }
- ]
- },
- {
- "name":"junior",
- "class":[
- {
- "no.":1,
- "students":41
- },
- {
- "no.":2,
- "students":42
- },
- {
- "no.":3,
- "students":43
- }
- ]
- }
- ]
- }
- }
- ]
然后,如果我要获取grade为sophomore的no.等于3的students数量,那么代码如下:
[java] view plain copy
- package orgjson;
- import java.io.File;
- import java.io.FileReader;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import org.json.JSONTokener;
- public class JSONTest {
- public static void main(String[] args) throws Exception {
- JSONTokener jsonTokener = new JSONTokener(new FileReader(new File("json.txt")));
- JSONArray jsonArray = new JSONArray(jsonTokener);//获取整个json文件的内容,因为最外层是数组,所以用JSONArray来构造
- System.out.println(jsonArray);
-
- //这个JSONArray数组只包含一个JSONObject对象,标为jsonObject1
- JSONObject jsonObject1 = jsonArray.getJSONObject(0);
- System.out.println(jsonObject1);
-
- //jsonObject1只包含一个institute字段,这里获取这个字段内容赋给jsonObject2
- JSONObject jsonObject2 = jsonObject1.getJSONObject("institute");
- System.out.println(jsonObject2);
-
- //jsonObject2包含name字段和grade字段,grade字段对应的是一个JSONArray数组
- String valueOfname = jsonObject2.getString("name");
- System.out.println(valueOfname);
- JSONArray jsonArray2 = jsonObject2.getJSONArray("grade");
- System.out.println(jsonArray2);
-
- //jsonArray2数组包含3个对象,每个对象里面有name字段和class字段,这里获取第二个对象
- JSONObject jsonObject3 = jsonArray2.getJSONObject(1);
- System.out.println(jsonObject3);
-
- //然后从jsonObject3对象里获取class字段对应的JSONArray数组
- JSONArray jsonArray3 = jsonObject3.getJSONArray("class");
- System.out.println(jsonArray3);
-
- //下面直接获取no.等于3的students数量,过程都一样
- int num = jsonArray3.getJSONObject(2).getInt("students");
- System.out.println("最后获取的结果为:" + num);
- }
- }
输出的结果如下,这里只截取控制台输出的前半部分结果:
5.结束语
之前解析json数据都是一直百度百度,别人怎么用就怎么用,做大作业时有时候会org.json包也导入,Gson包也导入,然后各用一点功能。现在想想其实只用一个org.json包就可以完全解决我的需求,只是之前一直没有总结各种用法,所以这两天看了下官网源码,总结了下学习笔记,还是收获蛮大的。
其实看官网api文档,然后自己动手实践各种方法才是最好的学习方式。百度别人的教程,有时候只能得到小部分的说明。
org.json包还有很多的api方法并没有用过,所以就不做笔记了。以后用到再继续完善。
- package orgjson;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.json.*;
- public class JsonArrayTest {
- public static void constructorTest() {
- String jsonStr = "[{'name':'JTZen9','age':21}]";
- JSONArray strJson = new JSONArray(jsonStr); // 传入字符串
- System.out.println("构造参数为String类:" + strJson);
- List
- for (int i = 1; i < 3; i++) {
- Map
""> map = new HashMap "">(); - map.put("title", "java程序设计 第" + i + "版");
- map.put("price", i*20);
- list.add(map);
- }
- JSONArray mapJson = new JSONArray(list); // 传入Collection类型
- System.out.println("构造参数为Collection类:" + mapJson);
- int[] numlist = new int[10];
- for (int i = 0; i < numlist.length; i++) {
- numlist[i] = i;
- }
- JSONArray arrayJson = new JSONArray(numlist); // 传入Array类型,实例1
- System.out.println(arrayJson);
- Student[] student = {new Student(),new Student()};
- student[0].setAge(21);
- student[0].setName("JTZen9");
- student[0].setSex("male");
- student[1].setAge(21);
- student[1].setName("heiheihei");
- student[1].setSex("female");
- JSONArray beanJson = new JSONArray(student); // 传入Array类型,实例2
- System.out.println("构造参数为Array类:" + beanJson);
- }
- public static void main(String[] args) {
- constructorTest();
- }
- }
view plain copy
- package orgjson;
- import java.util.HashMap;
- import java.util.Map;
- import org.json.*;
- public class JSONArrayTest {
- public static void putMethodTest() {
- JSONArray jsonArray1 = new JSONArray();
- jsonArray1.put("JTZen9");
- jsonArray1.put(21);
- jsonArray1.put("male");
- System.out.println(jsonArray1);
-
- JSONArray jsonArray2 = new JSONArray();
- Map
""> map = new HashMap"">();
- map.put("title", "java程序设计 第2版");
- map.put("price", 20);
- jsonArray2.put(map); //传入一个map
- System.out.println("传入一个Map:" + jsonArray2);
- map.clear();
- map.put("title", "java程序设计 第3版");
- map.put("price", 30);
- jsonArray2.put(map); //没有下标的直接在结果后面添加
- System.out.println("没有指定下标:" + jsonArray2);
- map.clear();
- map.put("title", "java程序设计 第1版");
- map.put("price", 10);
- jsonArray2.put(0,map); //使用下标可以添加到自定义的位置
- System.out.println("添加到第一个位置:" + jsonArray2);
-
- Student[] student = { new Student(), new Student() };
- student[0].setAge(21);
- student[0].setName("JTZen9");
- student[0].setSex("male");
- student[1].setAge(21);
- student[1].setName("heiheihei");
- student[1].setSex("female");
- JSONArray jsonArray3 = new JSONArray();
- jsonArray3.put(student);
- System.out.println("注意输出结果:" + jsonArray3);
-
- }
-
- public static void main(String[] args) {
- putMethodTest();
- }
- }
输出的结果如下:
view plain copy
- package orgjson;
- import java.io.PrintWriter;
- import java.util.HashMap;
- import java.util.Map;
- import org.json.*;
- public class JSONWriterStringerTest {
- public static void JSONStringerTest() throws Exception {
-
- PrintWriter writer = new PrintWriter("test.txt");
- JSONWriter jsonWriter = new JSONWriter(writer);
- jsonWriter.object().key("name").value("JTZen9").key("age").value(21).key("sex").value("male").endObject();
- writer.flush();
- writer.close();
-
- Map
""> map1 = new HashMap"">();
- map1.put("age", 21);
- map1.put("sex", "male");
- map1.put("name", "jtzen9");
- Map
""> map2 = new HashMap"">();
- map2.put("age", 21);
- map2.put("sex", "female");
- map2.put("name", "heiheihei");
- JSONStringer jsonStringer = new JSONStringer();
- jsonStringer.array().value(map1).value(map2).endArray();
- System.out.println(jsonStringer);
- }
-
- public static void main(String[] args) throws Exception {
- JSONStringerTest();
- }
- }
输出结果,上面为test.txt文件,下面为控制台:
view plain copy
- package orgjson;
- import java.io.File;
- import java.io.FileReader;
- import org.json.*;
- public class JSONTokenerTest {
-
- public static void readJsonFile() throws Exception{
- JSONTokener jsonTokener = new JSONTokener(new FileReader(new File("test.txt")));
- JSONObject jsonObject = new JSONObject(jsonTokener);
- System.out.println(jsonObject);
- System.out.println("姓名:" + jsonObject.getString("name"));
- System.out.println("年龄:" + jsonObject.getInt("age"));
- }
-
- public static void main(String[] args) throws Exception {
- readJsonFile();
- }
- }
输出结果如下:
view plain copy
- [
- {
- "institute":{
- "name":"Software Institute",
- "grade":[
- {
- "name":"freshman",
- "class":[
- {
- "no.":1,
- "students":61
- },
- {
- "no.":2,
- "students":62
- },
- {
- "no.":3,
- "students":63
- }
- ]
- },
- {
- "name":"sophomore",
- "class":[
- {
- "no.":1,
- "students":51
- },
- {
- "no.":2,
- "students":52
- },
- {
- "no.":3,
- "students":53
- }
- ]
- },
- {
- "name":"junior",
- "class":[
- {
- "no.":1,
- "students":41
- },
- {
- "no.":2,
- "students":42
- },
- {
- "no.":3,
- "students":43
- }
- ]
- }
- ]
- }
- }
- ]
然后,如果我要获取grade为sophomore的no.等于3的students数量,那么代码如下:
[java] view plain copy
- package orgjson;
- import java.io.File;
- import java.io.FileReader;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import org.json.JSONTokener;
- public class JSONTest {
- public static void main(String[] args) throws Exception {
- JSONTokener jsonTokener = new JSONTokener(new FileReader(new File("json.txt")));
- JSONArray jsonArray = new JSONArray(jsonTokener);//获取整个json文件的内容,因为最外层是数组,所以用JSONArray来构造
- System.out.println(jsonArray);
-
- //这个JSONArray数组只包含一个JSONObject对象,标为jsonObject1
- JSONObject jsonObject1 = jsonArray.getJSONObject(0);
- System.out.println(jsonObject1);
-
- //jsonObject1只包含一个institute字段,这里获取这个字段内容赋给jsonObject2
- JSONObject jsonObject2 = jsonObject1.getJSONObject("institute");
- System.out.println(jsonObject2);
-
- //jsonObject2包含name字段和grade字段,grade字段对应的是一个JSONArray数组
- String valueOfname = jsonObject2.getString("name");
- System.out.println(valueOfname);
- JSONArray jsonArray2 = jsonObject2.getJSONArray("grade");
- System.out.println(jsonArray2);
-
- //jsonArray2数组包含3个对象,每个对象里面有name字段和class字段,这里获取第二个对象
- JSONObject jsonObject3 = jsonArray2.getJSONObject(1);
- System.out.println(jsonObject3);
-
- //然后从jsonObject3对象里获取class字段对应的JSONArray数组
- JSONArray jsonArray3 = jsonObject3.getJSONArray("class");
- System.out.println(jsonArray3);
-
- //下面直接获取no.等于3的students数量,过程都一样
- int num = jsonArray3.getJSONObject(2).getInt("students");
- System.out.println("最后获取的结果为:" + num);
- }
- }
输出的结果如下,这里只截取控制台输出的前半部分结果:
5.结束语
之前解析json数据都是一直百度百度,别人怎么用就怎么用,做大作业时有时候会org.json包也导入,Gson包也导入,然后各用一点功能。现在想想其实只用一个org.json包就可以完全解决我的需求,只是之前一直没有总结各种用法,所以这两天看了下官网源码,总结了下学习笔记,还是收获蛮大的。
其实看官网api文档,然后自己动手实践各种方法才是最好的学习方式。百度别人的教程,有时候只能得到小部分的说明。
org.json包还有很多的api方法并没有用过,所以就不做笔记了。以后用到再继续完善。
- package orgjson;
- import java.io.PrintWriter;
- import java.util.HashMap;
- import java.util.Map;
- import org.json.*;
- public class JSONWriterStringerTest {
- public static void JSONStringerTest() throws Exception {
- PrintWriter writer = new PrintWriter("test.txt");
- JSONWriter jsonWriter = new JSONWriter(writer);
- jsonWriter.object().key("name").value("JTZen9").key("age").value(21).key("sex").value("male").endObject();
- writer.flush();
- writer.close();
- Map
""> map1 = new HashMap "">(); - map1.put("age", 21);
- map1.put("sex", "male");
- map1.put("name", "jtzen9");
- Map
""> map2 = new HashMap "">(); - map2.put("age", 21);
- map2.put("sex", "female");
- map2.put("name", "heiheihei");
- JSONStringer jsonStringer = new JSONStringer();
- jsonStringer.array().value(map1).value(map2).endArray();
- System.out.println(jsonStringer);
- }
- public static void main(String[] args) throws Exception {
- JSONStringerTest();
- }
- }
view plain copy
- package orgjson;
- import java.io.File;
- import java.io.FileReader;
- import org.json.*;
- public class JSONTokenerTest {
-
- public static void readJsonFile() throws Exception{
- JSONTokener jsonTokener = new JSONTokener(new FileReader(new File("test.txt")));
- JSONObject jsonObject = new JSONObject(jsonTokener);
- System.out.println(jsonObject);
- System.out.println("姓名:" + jsonObject.getString("name"));
- System.out.println("年龄:" + jsonObject.getInt("age"));
- }
-
- public static void main(String[] args) throws Exception {
- readJsonFile();
- }
- }
输出结果如下:
view plain copy
- [
- {
- "institute":{
- "name":"Software Institute",
- "grade":[
- {
- "name":"freshman",
- "class":[
- {
- "no.":1,
- "students":61
- },
- {
- "no.":2,
- "students":62
- },
- {
- "no.":3,
- "students":63
- }
- ]
- },
- {
- "name":"sophomore",
- "class":[
- {
- "no.":1,
- "students":51
- },
- {
- "no.":2,
- "students":52
- },
- {
- "no.":3,
- "students":53
- }
- ]
- },
- {
- "name":"junior",
- "class":[
- {
- "no.":1,
- "students":41
- },
- {
- "no.":2,
- "students":42
- },
- {
- "no.":3,
- "students":43
- }
- ]
- }
- ]
- }
- }
- ]
然后,如果我要获取grade为sophomore的no.等于3的students数量,那么代码如下:
[java] view plain copy
- package orgjson;
- import java.io.File;
- import java.io.FileReader;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import org.json.JSONTokener;
- public class JSONTest {
- public static void main(String[] args) throws Exception {
- JSONTokener jsonTokener = new JSONTokener(new FileReader(new File("json.txt")));
- JSONArray jsonArray = new JSONArray(jsonTokener);//获取整个json文件的内容,因为最外层是数组,所以用JSONArray来构造
- System.out.println(jsonArray);
-
- //这个JSONArray数组只包含一个JSONObject对象,标为jsonObject1
- JSONObject jsonObject1 = jsonArray.getJSONObject(0);
- System.out.println(jsonObject1);
-
- //jsonObject1只包含一个institute字段,这里获取这个字段内容赋给jsonObject2
- JSONObject jsonObject2 = jsonObject1.getJSONObject("institute");
- System.out.println(jsonObject2);
-
- //jsonObject2包含name字段和grade字段,grade字段对应的是一个JSONArray数组
- String valueOfname = jsonObject2.getString("name");
- System.out.println(valueOfname);
- JSONArray jsonArray2 = jsonObject2.getJSONArray("grade");
- System.out.println(jsonArray2);
-
- //jsonArray2数组包含3个对象,每个对象里面有name字段和class字段,这里获取第二个对象
- JSONObject jsonObject3 = jsonArray2.getJSONObject(1);
- System.out.println(jsonObject3);
-
- //然后从jsonObject3对象里获取class字段对应的JSONArray数组
- JSONArray jsonArray3 = jsonObject3.getJSONArray("class");
- System.out.println(jsonArray3);
-
- //下面直接获取no.等于3的students数量,过程都一样
- int num = jsonArray3.getJSONObject(2).getInt("students");
- System.out.println("最后获取的结果为:" + num);
- }
- }
输出的结果如下,这里只截取控制台输出的前半部分结果:
5.结束语
之前解析json数据都是一直百度百度,别人怎么用就怎么用,做大作业时有时候会org.json包也导入,Gson包也导入,然后各用一点功能。现在想想其实只用一个org.json包就可以完全解决我的需求,只是之前一直没有总结各种用法,所以这两天看了下官网源码,总结了下学习笔记,还是收获蛮大的。
其实看官网api文档,然后自己动手实践各种方法才是最好的学习方式。百度别人的教程,有时候只能得到小部分的说明。
org.json包还有很多的api方法并没有用过,所以就不做笔记了。以后用到再继续完善。
- [
- {
- "institute":{
- "name":"Software Institute",
- "grade":[
- {
- "name":"freshman",
- "class":[
- {
- "no.":1,
- "students":61
- },
- {
- "no.":2,
- "students":62
- },
- {
- "no.":3,
- "students":63
- }
- ]
- },
- {
- "name":"sophomore",
- "class":[
- {
- "no.":1,
- "students":51
- },
- {
- "no.":2,
- "students":52
- },
- {
- "no.":3,
- "students":53
- }
- ]
- },
- {
- "name":"junior",
- "class":[
- {
- "no.":1,
- "students":41
- },
- {
- "no.":2,
- "students":42
- },
- {
- "no.":3,
- "students":43
- }
- ]
- }
- ]
- }
- }
- ]
- package orgjson;
- import java.io.File;
- import java.io.FileReader;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import org.json.JSONTokener;
- public class JSONTest {
- public static void main(String[] args) throws Exception {
- JSONTokener jsonTokener = new JSONTokener(new FileReader(new File("json.txt")));
- JSONArray jsonArray = new JSONArray(jsonTokener);//获取整个json文件的内容,因为最外层是数组,所以用JSONArray来构造
- System.out.println(jsonArray);
- //这个JSONArray数组只包含一个JSONObject对象,标为jsonObject1
- JSONObject jsonObject1 = jsonArray.getJSONObject(0);
- System.out.println(jsonObject1);
- //jsonObject1只包含一个institute字段,这里获取这个字段内容赋给jsonObject2
- JSONObject jsonObject2 = jsonObject1.getJSONObject("institute");
- System.out.println(jsonObject2);
- //jsonObject2包含name字段和grade字段,grade字段对应的是一个JSONArray数组
- String valueOfname = jsonObject2.getString("name");
- System.out.println(valueOfname);
- JSONArray jsonArray2 = jsonObject2.getJSONArray("grade");
- System.out.println(jsonArray2);
- //jsonArray2数组包含3个对象,每个对象里面有name字段和class字段,这里获取第二个对象
- JSONObject jsonObject3 = jsonArray2.getJSONObject(1);
- System.out.println(jsonObject3);
- //然后从jsonObject3对象里获取class字段对应的JSONArray数组
- JSONArray jsonArray3 = jsonObject3.getJSONArray("class");
- System.out.println(jsonArray3);
- //下面直接获取no.等于3的students数量,过程都一样
- int num = jsonArray3.getJSONObject(2).getInt("students");
- System.out.println("最后获取的结果为:" + num);
- }
- }