如何使用Gson解析嵌套数组的json对象列表 [英] How to use Gson to parse a list of json objects with nested arrays

查看:156
本文介绍了如何使用Gson解析嵌套数组的json对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Gson的新手,对java不太熟悉。我有一个json对象列表,然后在每个对象内列出这样的列表:

  [
{
A:358584,
B:Apache,
C:[
种族,
小说种族,
美洲印第安人组织

D:23.647824738317627,
E:种族,
F:Apache
},
{
A:19283806,
B:圣何塞,
C:[
地点,
雇主,
地区,
过境服务区
],
D:11.184500611578535,
E:地点,
F:旧金山湾区
}
]

我会很感激一个详细的或一步一步的答案,如何做到这一点,以及使用哪些类和方法。

解决方案

尝试任何东西(有很多例子已经可以在线)?



使用google' https://github.com/google/gson '图书馆。

  JSON格式:< Attribute_Name> :<值> 
[] - >列表或数组
{} - >单个对象

现在创建一个Java POJO,就像JSON结构一样。

  class Data {
private Integer A;
私人字符串B;
私人列表< String> C;
私人双D;
私人字符串E;
私人字符串F;
}

现在使用GSON库解析字符串。

  public static void main(String [] args){
String data =[{\A \:358584,\\ B \:\阿帕奇\,\C \:[\种族\,\小说种族\,\美洲印第安人小组 ],\ D\ :23.647824738317627,\ E\ :\ Ethnicity\ \ F\:\ Apache\},{\ A \:19283806,\B \:\San Jose \,\C \:[\Location \,\Employer \,\\ \区域\,\运输服务区域\],\D \:11.184500611578535,\E \:\Location \,\F \ :\旧金山湾区\}];

Data [] dataArray =(new Gson())。fromJson(data,Data []。class);
if(dataArray!= null){
System.out.println(dataArray.length);
}
}


I am new to Gson and not very experienced with java. I have a list of json objects and then lists inside each object like this:

[
  {
    "A": 358584,
    "B": "Apache",
    "C": [
      "Ethnicity",
      "Ethnicity in fiction",
      "American Indian group"
    ],
    "D": 23.647824738317627,
    "E": "Ethnicity",
    "F": "Apache"
  },
  {
    "A": 19283806,
    "B": "San Jose",
    "C": [
      "Location",
      "Employer",
      "Region",
      "Transit Service Area"
    ],
    "D": 11.184500611578535,
    "E": "Location",
    "F": "San Francisco Bay Area"
  }
]

I would appreciate a detailed or step by step answer on how to do this and which classes and methods to use.

解决方案

Did you try anything already (There are plenty of examples already availalbe online)?

Use google 'https://github.com/google/gson' library.

JSON format: <Attribute_Name> : <Value>
[] --> List or Array
{} --> Single Object

So now create a Java POJO just like JSON structure.

class Data {
    private Integer A;
    private String B;
    private List<String> C;
    private Double D;
    private String E;
    private String F;
    }

Now use the GSON library to parse the string.

 public static void main(String[] args) {
    String data = "[{\"A\":358584,\"B\":\"Apache\",\"C\":[\"Ethnicity\",\"Ethnicity in fiction\",\"American Indian group\"],\"D\":23.647824738317627,\"E\":\"Ethnicity\",\"F\":\"Apache\"},{\"A\":19283806,\"B\":\"San Jose\",\"C\":[\"Location\",\"Employer\",\"Region\",\"Transit Service Area\"],\"D\":11.184500611578535,\"E\":\"Location\",\"F\":\"San Francisco Bay Area\"}]";

    Data[] dataArray = (new Gson()).fromJson(data, Data[].class);
    if (dataArray != null) {
        System.out.println(dataArray.length);
    }
    }

这篇关于如何使用Gson解析嵌套数组的json对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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