JSON 解析

2018-02-28  本文已影响0人  文文太远了

JSON解析三种方式:

解析方式 特点 适用场景
org.json 书写简便,消耗内存 小文档,频繁访问
Gson 按需解析,耗内存少,效率低于Jackson 按需解析
Jackson 大文档完全解析效率最高,必须完全解析文档 大文档,不用按需解析

下面简单说明一下三种解析方式如何使用

 

公共代码


json文档内容:

{
  "vegetable":["茄子", "番茄", "青菜"],
  "fruit":[
    {"name":"橘子","color":"橘色", "weight":"1"},
    {"name":"葡萄","color":"紫色", "weight":"2"},
    {"name":"苹果","color":"红色", "weight":"3"}
  ],
  "food":"米饭"
}

为了方便,在java直接用String形式书写:

String jsonString = "{\n" +
                "  \"vegetable\":[\"茄子\", \"番茄\", \"青菜\"],\n" +
                "  \"fruit\":[\n" +
                "    {\"name\":\"橘子\",\"color\":\"橘色\", \"weight\":\"1\"},\n" +
                "    {\"name\":\"葡萄\",\"color\":\"紫色\", \"weight\":\"2\"},\n" +
                "    {\"name\":\"苹果\",\"color\":\"红色\", \"weight\":\"3\"}\n" +
                "  ],\n" +
                "  \"food\":\"米饭\"\n" +
                "}";

 

三种解析方式用例


org.json 解析

JSONObject jsonObject = new JSONObject(jsonString);
JSONArray vegetable = jsonObject.getJSONArray("vegetable");
vegetable.getString(1);//番茄
JSONArray fruit = jsonObject.getJSONArray("fruit");
fruit.getJSONObject(2).getString("color");//红色
jsonObject.getString("food");//米饭

 
Gson 解析

public class EatBean {

    @SerializedName("food")
    private String food;
    @SerializedName("fruit") //这个注释对应json文档中对应属性key
    private List<FruitBean> fruitBeanList; //如果没有上面的注释,那这里的变量名称必须要对应json文档
    @SerializedName("vegetable")
    private List<String> vegetableList;

    public String getFood() {
        return food;
    }

    public void setFood(String food) {
        this.food = food;
    }

    public List<FruitBean> getFruitBeanList() { // get方法名可以改
        return fruitBeanList;
    }

    public void setFruit(List<FruitBean> fruitBeanList) { // set方法名改了Jackson解析会出错,因为这个方法不会调用到,尽量不要改
        this.fruitBeanList = fruitBeanList;
    }

    public List<String> getVegetableList() {
        return vegetableList;
    }

    public void setVegetable(List<String> vegetableList) {
        this.vegetableList = vegetableList;
    }

    public static class FruitBean {

        @SerializedName("color")
        private String color;
        @SerializedName("name")
        private String name;
        @SerializedName("weight")
        private String weight;

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getWeight() {
            return weight;
        }

        public void setWeight(String weight) {
            this.weight = weight;
        }
    }
}
implementation 'com.google.code.gson:gson:2.8.0'
Gson gson = new Gson();
EatBean eatBean = gson.fromJson(jsonString, EatBean.class);

这样json就被解析成了一个对象,使用gson解析时JavaBean对象不需要全部的属性都和json文档一一对应,按需编写即可,不需要的属性可以不写。

 
Jackson 解析

implementation files('libs/jackson-all-1.9.2.jar')
ObjectMapper objectMapper = new ObjectMapper();
EatBean eatBean = objectMapper.readValue(jsonString, EatBean.class);
上一篇下一篇

猜你喜欢

热点阅读