关闭

如何用Gson解析复杂的Json数据

359人阅读 评论(0) 收藏 举报

今天发现了个极好的网站,是用来自动将json数据分化为实体类的

http://api.stay4it.com/json/index.html


首先,在他自动转化为实体类的过程中,会出现

@JsonProperty("isEnd")
   private int isend;

这种类型的实体数据

将@JsonProperty("isEnd")删掉,并将private int isend;改为isEnd,然后重新get set即可;

此外,在创建实体类的过程中,某些数据的类型会被识别错误,所以在自动生成实体类后检查一遍即可;

当实体类创建完毕后,利用Gson的API解析即可,当JSON数据最外层为[  ],即数组类型的JSON数据时,如下解析:

List<Person> ps = gson.fromJson(str, new TypeToken<List<Person>>(){}.getType()); for(int i = 0; i < ps.size() ; i++) { Person p = ps.get(i); System.out.println(p.toString()); }
当JSON数据的最外层为{   },即Object的JSON数据时,如下解析:

Person person = gson.fromJson(str, Person.class);



当实体类中嵌套多层List< >数据时,在代码中如下编辑即可:

 List<Person> result = p.getResults();


最后附上完整代码,自行理解。

        package com.example.zhouyuchen.gsontest;
        
        
        import android.app.Activity;
        import android.os.Bundle;
        import android.util.Log;
        
        import com.google.gson.Gson;
        import com.google.gson.reflect.TypeToken;
        
        import java.util.List;
        
        public class MainActivity extends Activity {
            private String JSON = "[\n" +
                    "\n" +
                    "    {\n" +
                    "        \"id\":1,\n" +
                    "        \"count\":25,\n" +
                    "        \"isEnd\":0,\n" +
                    "        \"children\":[\n" +
                    "            {\n" +
                    "                \"id\":57,\n" +
                    "                \"count\":3,\n" +
                    "                \"isEnd\":0,\n" +
                    "                \"parentInditarator\":1,\n" +
                    "                \"children\":[\n" +
                    "                    {\n" +
                    "                        \"id\":150,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":57,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"cyxxs57\",\n" +
                    "                        \"targetName\":\"查阅信息数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":149,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":57,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"jdrc57\",\n" +
                    "                        \"targetName\":\"接待人次\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":148,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":57,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"cyds57\",\n" +
                    "                        \"targetName\":\"查阅点数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    }\n" +
                    "                ],\n" +
                    "                \"topInditarator\":1,\n" +
                    "                \"code\":\"tsg\",\n" +
                    "                \"targetName\":\"图书馆\",\n" +
                    "                \"targetCategory\":2,\n" +
                    "                \"size\":0\n" +
                    "            },\n" +
                    "            {\n" +
                    "                \"id\":56,\n" +
                    "                \"count\":3,\n" +
                    "                \"isEnd\":0,\n" +
                    "                \"parentInditarator\":1,\n" +
                    "                \"children\":[\n" +
                    "                    {\n" +
                    "                        \"id\":147,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":56,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"cyxxs56\",\n" +
                    "                        \"targetName\":\"查阅信息数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":146,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":56,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"jdrc56\",\n" +
                    "                        \"targetName\":\"接待人次\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":145,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":56,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"cyds56\",\n" +
                    "                        \"targetName\":\"查阅点数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    }\n" +
                    "                ],\n" +
                    "                \"topInditarator\":1,\n" +
                    "                \"code\":\"dag\",\n" +
                    "                \"targetName\":\"档案馆\",\n" +
                    "                \"targetCategory\":2,\n" +
                    "                \"size\":0\n" +
                    "            },\n" +
                    "            {\n" +
                    "                \"id\":55,\n" +
                    "                \"count\":3,\n" +
                    "                \"isEnd\":0,\n" +
                    "                \"parentInditarator\":1,\n" +
                    "                \"children\":[\n" +
                    "                    {\n" +
                    "                        \"id\":144,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":55,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"cyxxs55\",\n" +
                    "                        \"targetName\":\"查阅信息数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":143,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":55,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"jdrc55\",\n" +
                    "                        \"targetName\":\"接待人次\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":142,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":55,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"cyds55\",\n" +
                    "                        \"targetName\":\"查阅点数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    }\n" +
                    "                ],\n" +
                    "                \"topInditarator\":1,\n" +
                    "                \"code\":\"zwfwzx\",\n" +
                    "                \"targetName\":\"政务服务中心\",\n" +
                    "                \"targetCategory\":2,\n" +
                    "                \"size\":0\n" +
                    "            },\n" +
                    "            {\n" +
                    "                \"id\":54,\n" +
                    "                \"count\":1,\n" +
                    "                \"isEnd\":1,\n" +
                    "                \"parentInditarator\":1,\n" +
                    "                \"topInditarator\":1,\n" +
                    "                \"code\":\"qttjzdgkzfxxts\",\n" +
                    "                \"targetName\":\"其他途径主动公开政府信息条数\",\n" +
                    "                \"targetCategory\":2,\n" +
                    "                \"size\":0\n" +
                    "            },\n" +
                    "            {\n" +
                    "                \"id\":53,\n" +
                    "                \"count\":3,\n" +
                    "                \"isEnd\":0,\n" +
                    "                \"parentInditarator\":1,\n" +
                    "                \"children\":[\n" +
                    "                    {\n" +
                    "                        \"id\":141,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":53,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"gkzfxxts53\",\n" +
                    "                        \"targetName\":\"公开政府信息条数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":140,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":53,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"zfgbckqs\",\n" +
                    "                        \"targetName\":\"政府公报出刊期数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":139,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":53,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"byzfgbs\",\n" +
                    "                        \"targetName\":\"办有政府公报数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    }\n" +
                    "                ],\n" +
                    "                \"topInditarator\":1,\n" +
                    "                \"code\":\"zfgbgkqk\",\n" +
                    "                \"targetName\":\"政府公报公开情况\",\n" +
                    "                \"targetCategory\":2,\n" +
                    "                \"size\":0\n" +
                    "            },\n" +
                    "            {\n" +
                    "                \"id\":52,\n" +
                    "                \"count\":3,\n" +
                    "                \"isEnd\":0,\n" +
                    "                \"parentInditarator\":1,\n" +
                    "                \"children\":[\n" +
                    "                    {\n" +
                    "                        \"id\":138,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":52,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"zjxxgksj52\",\n" +
                    "                        \"targetName\":\"最近信息公开时间\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":137,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":52,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"gkzfxxts52\",\n" +
                    "                        \"targetName\":\"公开政府信息条数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":136,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":52,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"ktzwwxgs\",\n" +
                    "                        \"targetName\":\"开通政务微信个数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    }\n" +
                    "                ],\n" +
                    "                \"topInditarator\":1,\n" +
                    "                \"code\":\"zfwxgkqk\",\n" +
                    "                \"targetName\":\"政务微信公开情况\",\n" +
                    "                \"targetCategory\":2,\n" +
                    "                \"size\":0\n" +
                    "            },\n" +
                    "            {\n" +
                    "                \"id\":51,\n" +
                    "                \"count\":3,\n" +
                    "                \"isEnd\":0,\n" +
                    "                \"parentInditarator\":1,\n" +
                    "                \"children\":[\n" +
                    "                    {\n" +
                    "                        \"id\":135,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":51,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"zjgksj51\",\n" +
                    "                        \"targetName\":\"最近信息公开时间\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":134,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":51,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"gkzfxxts51\",\n" +
                    "                        \"targetName\":\"公开政府信息条数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":133,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":51,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"ktzwwbgs\",\n" +
                    "                        \"targetName\":\"开通政务微博个数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    }\n" +
                    "                ],\n" +
                    "                \"topInditarator\":1,\n" +
                    "                \"code\":\"zfwbgkqk\",\n" +
                    "                \"targetName\":\"政务微博公开情况\",\n" +
                    "                \"targetCategory\":2,\n" +
                    "                \"size\":0\n" +
                    "            },\n" +
                    "            {\n" +
                    "                \"id\":50,\n" +
                    "                \"count\":5,\n" +
                    "                \"isEnd\":0,\n" +
                    "                \"parentInditarator\":1,\n" +
                    "                \"children\":[\n" +
                    "                    {\n" +
                    "                        \"id\":111,\n" +
                    "                        \"count\":2,\n" +
                    "                        \"isEnd\":0,\n" +
                    "                        \"parentInditarator\":50,\n" +
                    "                        \"children\":[\n" +
                    "                            {\n" +
                    "                                \"id\":113,\n" +
                    "                                \"count\":1,\n" +
                    "                                \"isEnd\":1,\n" +
                    "                                \"parentInditarator\":111,\n" +
                    "                                \"topInditarator\":1,\n" +
                    "                                \"code\":\"zjxxgksj\",\n" +
                    "                                \"targetName\":\"最近信息公开时间\",\n" +
                    "                                \"targetCategory\":4,\n" +
                    "                                \"size\":0\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                                \"id\":112,\n" +
                    "                                \"count\":1,\n" +
                    "                                \"isEnd\":1,\n" +
                    "                                \"parentInditarator\":111,\n" +
                    "                                \"topInditarator\":1,\n" +
                    "                                \"code\":\"zfxxgkmlgks\",\n" +
                    "                                \"targetName\":\"政府信息公开目录公开数\",\n" +
                    "                                \"targetCategory\":4,\n" +
                    "                                \"size\":0\n" +
                    "                            }\n" +
                    "                        ],\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"zfxxgkmlgkqk\",\n" +
                    "                        \"targetName\":\"政府信息公开目录公开情况\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":110,\n" +
                    "                        \"count\":1,\n" +
                    "                        \"isEnd\":1,\n" +
                    "                        \"parentInditarator\":50,\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"zfwzgks\",\n" +
                    "                        \"targetName\":\"政府网站公开数\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\":130,\n" +
                    "                        \"count\":2,\n" +
                    "                        \"isEnd\":0,\n" +
                    "                        \"parentInditarator\":50,\n" +
                    "                        \"children\":[\n" +
                    "                            {\n" +
                    "                                \"id\":132,\n" +
                    "                                \"count\":1,\n" +
                    "                                \"isEnd\":1,\n" +
                    "                                \"parentInditarator\":130,\n" +
                    "                                \"topInditarator\":1,\n" +
                    "                                \"code\":\"zjgksj130\",\n" +
                    "                                \"targetName\":\"最近信息公开时间\",\n" +
                    "                                \"targetCategory\":4,\n" +
                    "                                \"size\":0\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                                \"id\":131,\n" +
                    "                                \"count\":1,\n" +
                    "                                \"isEnd\":1,\n" +
                    "                                \"parentInditarator\":130,\n" +
                    "                                \"topInditarator\":1,\n" +
                    "                                \"code\":\"wztgmlwgks\",\n" +
                    "                                \"targetName\":\"网站通过目录外公开数\",\n" +
                    "                                \"targetCategory\":4,\n" +
                    "                                \"size\":0\n" +
                    "                            }\n" +
                    "                        ],\n" +
                    "                        \"topInditarator\":1,\n" +
                    "                        \"code\":\"wztgmlwgkqk\",\n" +
                    "                        \"targetName\":\"网站通过目录外公开情况\",\n" +
                    "                        \"targetCategory\":3,\n" +
                    "                        \"size\":0\n" +
                    "                    }\n" +
                    "                ],\n" +
                    "                \"topInditarator\":1,\n" +
                    "                \"code\":\"zfwzgkqk\",\n" +
                    "                \"targetName\":\"政府网站公开情况\",\n" +
                    "                \"targetCategory\":2,\n" +
                    "                \"size\":0\n" +
                    "            },\n" +
                    "            {\n" +
                    "                \"id\":49,\n" +
                    "                \"count\":1,\n" +
                    "                \"isEnd\":1,\n" +
                    "                \"parentInditarator\":1,\n" +
                    "                \"topInditarator\":1,\n" +
                    "                \"code\":\"zdgkzfxxzts\",\n" +
                    "                \"targetName\":\"主动公开政府信息总条数\",\n" +
                    "                \"targetCategory\":2,\n" +
                    "                \"size\":0\n" +
                    "            }\n" +
                    "        ],\n" +
                    "        \"code\":\"zdgk\",\n" +
                    "        \"targetName\":\"主动公开\",\n" +
                    "        \"targetCategory\":1,\n" +
                    "        \"size\":0\n" +
                    "    }\n" +
                    "\n" +
                    "]";
        
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                Gson gson = new Gson();
                List<Gsonbean> g = gson.fromJson(JSON, new TypeToken<List<Gsonbean>>() {
                }.getType());
                String a = g.get(0).getChildren().get(0).getChildren().get(0).getCode();
                Log.i("asdzxc", a+"");
        
            }
        
        
        }

package com.example.zhouyuchen.gsontest;

import java.util.List;


public class Gsonbean {

    private int id;
    private int count;
    private int isEnd;
    private List<Children> children;
    private String code;
    private String targetName;
    private int targetCategory;
    private int size;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getIsEnd() {
        return isEnd;
    }

    public void setIsEnd(int isEnd) {
        this.isEnd = isEnd;
    }

    public List<Children> getChildren() {
        return children;
    }

    public void setChildren(List<Children> children) {
        this.children = children;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getTargetName() {
        return targetName;
    }

    public void setTargetName(String targetName) {
        this.targetName = targetName;
    }

    public int getTargetCategory() {
        return targetCategory;
    }

    public void setTargetCategory(int targetCategory) {
        this.targetCategory = targetCategory;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}

package com.example.zhouyuchen.gsontest;


import java.util.List;

public class Children {

    private int id;
    private int count;
    private int isEnd;
    private int parentInditarator;
    private int topInditarator;
    private String code;
    private String targetName;
    private int targetCategory;
    private int size;
    private List<Children>children;

    public List<Children> getChildren() {
        return children;
    }

    public void setChildren(List<Children> children) {
        this.children = children;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getIsEnd() {
        return isEnd;
    }

    public void setIsEnd(int isEnd) {
        this.isEnd = isEnd;
    }

    public int getParentInditarator() {
        return parentInditarator;
    }

    public void setParentInditarator(int parentInditarator) {
        this.parentInditarator = parentInditarator;
    }

    public int getTopInditarator() {
        return topInditarator;
    }

    public void setTopInditarator(int topInditarator) {
        this.topInditarator = topInditarator;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getTargetName() {
        return targetName;
    }

    public void setTargetName(String targetName) {
        this.targetName = targetName;
    }

    public int getTargetCategory() {
        return targetCategory;
    }

    public void setTargetCategory(int targetCategory) {
        this.targetCategory = targetCategory;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8539次
    • 积分:206
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    文章存档
    最新评论