• 原始的JSON解析方式:

构造JSON对象 -> 判断JSON对象是否包含某个元素 -> 获取JSON对象里面的元素。


String content = "{\"name\":\"aceding\",\"age\":26}";
JSONObject jsonObject = new JSONObject(content);
if(jsonObject.has("name")){
	String name = jsonObject.getString("name");
}
if(jsonObject.has("age")){
	int age = jsonObject.getInt("age");
}


 元素很多的话,解析代码会写的很累。且不同内容的JSON对象要一一编写解析代码。

  • 进阶的JSON解析方式:

使用第三方JSON库进行解析,如Gson、Jackson、FastJson等。

以Gson为例,简单介绍一下其解析方法:

1. 先定义JSON数据对应的Entity类。


public class User {
    public String name;
    public int age;
    public String emailAddress;
}


 2. 解析JSON数据。


Gson gson = new Gson();
String jsonStr = "{\"name\":\"aceding\",\"age\":26}";
User user = gson.fromJson(jsonStr, User.class);


 相比于原始的解析方式,方便了些,但还需要程序员自己实现Entity类,且无法解析复杂的JSON体,如包含JSONArray和JSONObject的JSON体。

  • 懒人的JSON解析方式:

1. JSON数据对应的Entity类,一行代码直接生成。


// 解析json文件,生成对应的.java文件。
JsonParser.parseJsonStr(jsonStr);


jsonStr的内容:


{
    "className": "Ace",
    "packageName": "com.ace.lazyjson.gen",
    "name": "ace",
    "age": 27,
    "company": {
        "first": "oppo",
        "second": "tencent"
    },
    "hobby": [
        {
            "young": "playgame",
            "little": "sleep"
        },
        {
            "now": "gril"
        },
        "fish",
        [
            "read",
            {
                "game": "dota"
            },
            "eat"
        ],
        [
            "bike",
            {
                "work": "code"
            },
            "sleep"
        ]
    ]
}


 生成的.java文件如下:


2. JSON数据的解析,一行代码直接完成解析。


//解析json内容,生成对象。
Ace ace = Ace.convertFrom(jsonStr);


验证代码:


System.out.println("name: " + ace.name);
System.out.println("age: " + ace.age);
System.out.println("company first: " + ace.company.first);
System.out.println("company second: " + ace.company.second);

Object[] objs = ace.hobby;
Hobby hobbyOne = (Hobby) objs[0];
Hobby hobbyTwo = (Hobby) objs[1];
String hobbyThree = (String) objs[2];
Object[] hobbyFour = (Object[]) objs[3];
Object[] hobbyFive = (Object[]) objs[4];

System.out.println("hobby index 0 little/now/young: " + hobbyOne.little + " " + hobbyOne.now + " " + hobbyOne.young);
System.out.println("hobby index 1 little/now/young: " + hobbyTwo.little + " " + hobbyTwo.now + " " + hobbyTwo.young);
System.out.println("hobby index 2: " + hobbyThree);

String hobbyFourChildOne = (String) hobbyFour[0];
HobbyIndexThree hobbyFourChildTwo = (HobbyIndexThree) hobbyFour[1];
String hobbyFourChildThree = (String) hobbyFour[2];

System.out.println("hobby index 3 child index 0: " + hobbyFourChildOne);
System.out.println("hobby index 3 child index 1 game: " + hobbyFourChildTwo.game);
System.out.println("hobby index 3 child index 2: " + hobbyFourChildThree);
		
String hobbyFiveChildOne = (String) hobbyFive[0];
HobbyIndexFour hobbyFiveChildTwo = (HobbyIndexFour) hobbyFive[1];
String hobbyFiveChildThree = (String) hobbyFive[2];
		
System.out.println("hobby index 4 child index 0: " + hobbyFiveChildOne);
System.out.println("hobby index 4 child index 1 work: " + hobbyFiveChildTwo.work);
System.out.println("hobby index 4 child index 2: " + hobbyFiveChildThree);


运行后打印:解析OK!


相比于Gson,懒人的JSON解析方式不需要程序员自己编写entity类,且支持复杂的JSON体解析,另外通用的解析方式,支持不同内容的JSON体的解析。

  • 懒人的JSON解析方式的实现:

1. JSON数据对应的Entity类的自动生成:

深度遍历,每一个JSONObject生成一个类,使用FreeMaker生成.Java文件。

JSONObject对应Entity类的生成规则:

基础数据类型对应基础FiledType,JSONObject对应一个类,JSONArray对应Object[]。

JSONArray里面的JSONObject对应Entity类的生成规则:

JSONArray里面的JSONArray里面的JSONObject的Entity类生成规则:


2. JSON数据的自动解析:

通过反射的方式获取Entity类的fields,然后深度遍历赋值。

code:https://github.com/aceding/LazyJson

改进计划:JSONObject里面JSONObject作为静态内部类生成,而不是一个单独的Class存在。