如何解析JSON阵列由GSON多个对象? [英] How parse json array with multiple objects by gson?

查看:264
本文介绍了如何解析JSON阵列由GSON多个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何解析JSON使用GSON?我有多个对象类型JSON数组,我不知道我需要什么样的对象创建保存这个结构。我不能改变的JSON数据格式(我不控制服务器)。
我可以使用GSON或其他库解析此JSON数组,我该怎么办?

此JSON code座:

  [
  {
    类型:1,
    对象:{
      TITLE1:TITLE1,
      标题2:标题2
    }
  },
  {
    类型:2,
    对象:[
      串,
      串,
      串
    ]
  },
  {
    类型:3,
    对象:[
      {
        URL:URL,
        文:文本,
        宽:600,
        高度:600
      },
      {
        URL:URL,
        文:文本,
        宽:600,
        高度:600
      }
    ]
  },
  {
    类型:4,
    对象:{
      ID:337203,
      类型:1,
      城市:1
    }
  }
]


解决方案

这JSON结构本质上是GSON不友好。即你不能在干净的java因为对象键指的是动态类型建模。你可以用这个结构做的最好的是它的模型像这样:

 公共类模型扩展的ArrayList< Models.Container> {    公共类容器{
        公众诠释类型;
        公共Object对象;
    }    公共类Type1Object {
        公共字符串TITLE1;
        公共字符串TITLE2;
    }    公共类Type3Object {
        公共字符串的URL;
        公共字符串文本;
        公众诠释宽度;
        公众诠释高度;
    }    公共类Type4Object {
        公众诠释身份证;
        公众诠释类型;
        公众诠释城市;
    }}

然后做类型有些别扭开关和对象字段:

  JSON字符串={... JSON字符串...};
GSON GSON =新GSON();
模型模型= gson.fromJson(JSON,Models.class);
对于(Models.Container容器:模型){    字符串innerJson = gson.toJson(container.object);    开关(container.type){
        情况1:
            Models.Type1Object type1Object = gson.fromJson(innerJson,Models.Type1Object.class);
            //做1型对象的东西...
            打破;
        案例2:
            的String [] = type2Object gson.fromJson(innerJson,字符串[]类。);
            //做2类型的对象东西...
            打破;
        案例3:
            Models.Type3Object [] = type3Object gson.fromJson(innerJson,Models.Type3Object []类);
            //做3类对象的东西...
            打破;
        情况4:
            Models.Type4Object type4Object = gson.fromJson(innerJson,Models.Type4Object.class);
            //做4类对象的东西...
            打破;    }
}

最终,最好的解决办法是让改变的东西与Java兼容更多的JSON结构。

例如:

  [
  {
    类型:1,
    type1Object:{
      TITLE1:TITLE1,
      标题2:标题2
    }
  },
  {
    类型:2,
    type2Object:[
      串,
      串,
      串
    ]
  },
  {
    类型:3,
    type3Object:[
      {
        URL:URL,
        文:文本,
        宽:600,
        高度:600
      },
      {
        URL:URL,
        文:文本,
        宽:600,
        高度:600
      }
    ]
  },
  {
    类型:4,
    type4Object:{
      ID:337203,
      类型:1,
      城市:1
    }
  }
]

how can i parse json using gson? i have a json array with multiple object types and i don't know what kind of object i need to create to save this structure. I cannot change the json data format (I don't control the server). can i use gson or other library parse this json array, how should i do?

this json code block :

[
  {
    "type": 1,
    "object": {
      "title1": "title1",
      "title2": "title2"
    }
  },
  {
    "type": 2,
    "object": [
      "string",
      "string",
      "string"
    ]
  },
  {
    "type": 3,
    "object": [
      {
        "url": "url",
        "text": "text",
        "width": 600,
        "height": 600
      },
      {
        "url": "url",
        "text": "text",
        "width": 600,
        "height": 600
      }
    ]
  },
  {
    "type": 4,
    "object": {
      "id": 337203,
      "type": 1,
      "city": "1"
    }
  }
]

解决方案

This json structure is inherently gson-unfriendly. i.e You cannot model this cleanly in java because the "object" key refers to a dynamic type. The best you can do with this structure is model it like so:

    public class Models extends ArrayList<Models.Container> {

    public class Container {
        public int type;
        public Object object;
    }

    public class Type1Object {
        public String title1;
        public String title2;
    }

    public class Type3Object {
        public String url;
        public String text;
        public int width;
        public int height;
    }

    public class Type4Object {
        public int id;
        public int type;
        public int city;
    }

}

Then do some awkward switch on type and the object field:

String json = "{ ... json string ... }";
Gson gson = new Gson();
Models model = gson.fromJson(json, Models.class);


for (Models.Container container : model) {

    String innerJson = gson.toJson(container.object);

    switch(container.type){
        case 1:
            Models.Type1Object type1Object = gson.fromJson(innerJson, Models.Type1Object.class);
            // do something with type 1 object...                                
            break;
        case 2:
            String[] type2Object = gson.fromJson(innerJson, String[].class);
            // do something with type 2 object...
            break;
        case 3:
            Models.Type3Object[] type3Object = gson.fromJson(innerJson, Models.Type3Object[].class);
            // do something with type 3 object...
            break;
        case 4:
            Models.Type4Object type4Object = gson.fromJson(innerJson, Models.Type4Object.class);
            // do something with type 4 object...
            break;

    }
}

Ultimately the best solution is to get the json structure changed to something more compatible with java.

E.g:

[
  {
    "type": 1,
    "type1Object": {
      "title1": "title1",
      "title2": "title2"
    }
  },
  {
    "type": 2,
    "type2Object": [
      "string",
      "string",
      "string"
    ]
  },
  {
    "type": 3,
    "type3Object": [
      {
        "url": "url",
        "text": "text",
        "width": 600,
        "height": 600
      },
      {
        "url": "url",
        "text": "text",
        "width": 600,
        "height": 600
      }
    ]
  },
  {
    "type": 4,
    "type4Object": {
      "id": 337203,
      "type": 1,
      "city": "1"
    }
  }
]

这篇关于如何解析JSON阵列由GSON多个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆