# 第01章 org.json

# 1.1 JSON-Java API

JSONObject原生put方法构建JSON

JSON官网 http://json.org/ JSON-java API: http://stleary.github.io/JSON-java/index.html

1550920238998

1550920263523

1550920272927

包的引入(gradle):

// https://mvnrepository.com/artifact/org.json/json
compile group: 'org.json', name: 'json', version: '20180130'
1
2

使用程序创建一个json对象如下:

1550920312667

1550920320476

package com.changan.org_json;

import org.json.JSONObject;

public class JSONObjectSample {
    public static void main(String[] args) {
        jsonObjectTest();
    }

    private static void jsonObjectTest() {
        JSONObject wangxiaoer = new JSONObject();
        Object nullObj = null;
        wangxiaoer.put("name", "王小二");
        wangxiaoer.put("age", 25.2);
        wangxiaoer.put("birthday", "1990-08-23");
        wangxiaoer.put("school", "蓝翔");
        wangxiaoer.put("major", new String[]{"理发", "挖掘机"});
        wangxiaoer.put("has_girlfriend", nullObj);
        wangxiaoer.put("car", nullObj);
        wangxiaoer.put("house", nullObj);
        wangxiaoer.put("comment", "这是一个注释");
        System.out.println(wangxiaoer.toString());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

输出结果:

1550920358041

# 1.2 使用Map构建JSON

JSONObject(java.util.Map<?,?> map) 使用Map构建一个JSONObject

package com.changan.org_json;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class JSONObjectSample {
    public static void main(String[] args) {
        //jsonObjectTest();
        createJsonByMap();
    }

    private static void jsonObjectTest() {
        JSONObject wangxiaoer = new JSONObject();
        Object nullObj = null;
        wangxiaoer.put("name", "王小二");
        wangxiaoer.put("age", 25.2);
        wangxiaoer.put("birthday", "1990-08-23");
        wangxiaoer.put("school", "蓝翔");
        wangxiaoer.put("major", new String[]{"理发", "挖掘机"});
        wangxiaoer.put("has_girlfriend", nullObj);
        wangxiaoer.put("car", nullObj);
        wangxiaoer.put("house", nullObj);
        wangxiaoer.put("comment", "这是一个注释");
        System.out.println(wangxiaoer.toString());
    }

    private static void createJsonByMap() {
        Map<String, Object> wangxiaoer = new HashMap<>();
        Object nullObj = null;
        wangxiaoer.put("name", "王小二");
        wangxiaoer.put("age", 25.2);
        wangxiaoer.put("birthday", "1990-08-23");
        wangxiaoer.put("school", "蓝翔");
        wangxiaoer.put("major", new String[]{"理发", "挖掘机"});
        wangxiaoer.put("has_girlfriend", nullObj);
        wangxiaoer.put("car", nullObj);
        wangxiaoer.put("house", nullObj);
        wangxiaoer.put("comment", "这是一个注释");
        System.out.println(new JSONObject(wangxiaoer).toString());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# 1.3 使用JavaBean构建JSONObject

(实际开发中使用较多)

JSONObject(java.lang.Object bean) 使用对象的Bean属性的getter方法构建一个JSONObject Construct a JSONObject from an Object using bean getters. 如果不生成getter方法则输出为{}

package com.changan.org_json;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class JSONObjectSample {
    public static void main(String[] args) {
        //jsonObjectTest();
        //createJsonByMap();
        createJsonByBean();
    }

    private static void jsonObjectTest() {
        JSONObject wangxiaoer = new JSONObject();
        Object nullObj = null;
        wangxiaoer.put("name", "王小二");
        wangxiaoer.put("age", 25.2);
        wangxiaoer.put("birthday", "1990-08-23");
        wangxiaoer.put("school", "蓝翔");
        wangxiaoer.put("major", new String[]{"理发", "挖掘机"});
        wangxiaoer.put("has_girlfriend", nullObj);
        wangxiaoer.put("car", nullObj);
        wangxiaoer.put("house", nullObj);
        wangxiaoer.put("comment", "这是一个注释");
        System.out.println(wangxiaoer.toString());
    }

    private static void createJsonByMap() {
        Map<String, Object> wangxiaoer = new HashMap<>();
        Object nullObj = null;
        wangxiaoer.put("name", "王小二");
        wangxiaoer.put("age", 25.2);
        wangxiaoer.put("birthday", "1990-08-23");
        wangxiaoer.put("school", "蓝翔");
        wangxiaoer.put("major", new String[]{"理发", "挖掘机"});
        wangxiaoer.put("has_girlfriend", nullObj);
        wangxiaoer.put("car", nullObj);
        wangxiaoer.put("house", nullObj);
        wangxiaoer.put("comment", "这是一个注释");
        System.out.println(new JSONObject(wangxiaoer).toString());
    }

    private static void createJsonByBean() {
        Person wangxiaoer = new Person();
        wangxiaoer.setName("王小二");
        wangxiaoer.setAge(25.2);
        wangxiaoer.setBirthday("1990-08-23");
        wangxiaoer.setSchool("蓝翔");
        wangxiaoer.setMajor(new String[]{"理发", "挖掘机"});
        wangxiaoer.setHas_girfriend(false);
        wangxiaoer.setCar(null);
        wangxiaoer.setHouse(null);
        wangxiaoer.setComment("这是一个注释");
        System.out.println(new JSONObject(wangxiaoer));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

对象:

package com.changan.org_json;

public class Person {
    private String name;
    private String school;
    private boolean has_girfriend;
    private double age;
    private Object car;
    private Object house;
    private String[] major;
    private String comment;
    private String birthday;
	// getter & setter
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 1.4 从文件中读取JSON(JSON解析)

JSONObject(java.lang.String source) Construct a JSONObject from a source JSON text string. 通过文本构建JSONObject 依赖jar包:commons-io 创建一个文件:wangxiaoer.json,如下

{
  "birthday":"1990-08-23",
  "has_girfriend":false,
  "major":[
    "理发",
    "挖掘机"
  ],
  "school":"蓝翔",
  "name":"王小二",
  "comment":"这是一个注释",
  "age":25.2
}
1
2
3
4
5
6
7
8
9
10
11
12

添加apache.common依赖

// https://mvnrepository.com/artifact/commons-io/commons-io
compile group: 'commons-io', name: 'commons-io', version: '2.6'
1
2

代码:

package com.changan.org_json;

import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;

public class ReadJsonSample {
    public static void main(String[] args) throws IOException {
        File file = new File(ReadJsonSample.class.getResource("/wangxiaoer.json").getFile());
        String content = FileUtils.readFileToString(file, "UTF-8");
        JSONObject jsonObject = new JSONObject(content);
        System.out.println("姓名 = " + jsonObject.getString("name"));
        System.out.println("年龄 = " + jsonObject.getDouble("age"));
        System.out.println("有女朋友吗? = " + jsonObject.getBoolean("has_girlfriend"));
        //获取数组使用JSONArray
        JSONArray majorArray = jsonObject.getJSONArray("major");
        for (int i = 0; i < majorArray.length(); i++) {
            String major = (String)majorArray.get(i);
            System.out.println("专业"+(i+1)+":"+major);
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# 1.5 从文件读取JSON判断null

boolean isNull(java.lang.String key) Determine if the value associated with(与什么关联) the key is null or if there is no value. 判断与该key关联的值是否是null,是则返回true,不是null则返回false

package com.changan.org_json;

import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;

public class ReadJsonSample {
    public static void main(String[] args) throws IOException {
        File file = new File(ReadJsonSample.class.getResource("/wangxiaoer.json").getFile());
        String content = FileUtils.readFileToString(file, "UTF-8");
        JSONObject jsonObject = new JSONObject(content);
        if (!jsonObject.isNull("name")) {
            System.out.println("姓名 = " + jsonObject.getString("name"));
        }
        if (!jsonObject.isNull("age")) {
            System.out.println("年龄 = " + jsonObject.getDouble("age"));
        }
        if (!jsonObject.isNull("has_girlfriend")) {
            System.out.println("有女朋友吗? = " + jsonObject.getBoolean("has_girlfriend"));
        }
        //获取数组使用JSONArray
        JSONArray majorArray = jsonObject.getJSONArray("major");
        for (int i = 0; i < majorArray.length(); i++) {
            String major = (String)majorArray.get(i);
            System.out.println("专业"+(i+1)+":"+major);
        }
    }
} 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Last Updated: 10/20/2019, 11:49:45 PM