Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaGSON解析-数组和嵌套类_Java_Json_Gson - Fatal编程技术网

JavaGSON解析-数组和嵌套类

JavaGSON解析-数组和嵌套类,java,json,gson,Java,Json,Gson,我正在调用第三方REST服务以获取求职者的详细信息,它将以JSON格式返回响应。JSON数据看起来像 [ { "Id": 1 "County": "sample string 16", "Country": "sample string 17", "AtAddressSinceMonth": 1, "AtAddressSinceYear": 1, "AtAddressUntilMonth": 1, "AtAddressUntilYear": 1, "Dat

我正在调用第三方REST服务以获取求职者的详细信息,它将以JSON格式返回响应。JSON数据看起来像

[
  {
   "Id": 1
  "County": "sample string 16",
  "Country": "sample string 17",
  "AtAddressSinceMonth": 1,
  "AtAddressSinceYear": 1,
  "AtAddressUntilMonth": 1,
  "AtAddressUntilYear": 1,
   "DateOfBirth": "2013-12-11T14:25:27.3753327-05:00",
   "Documents": [
      {
        "Type": "sample string 1",
        "Description": "sample string 2",
        "File": "sample string 3"
       },
      {
        "Type": "sample string 1",
        "Description": "sample string 2",
        "File": "sample string 3"
      },

    ],
    "Events": [
      {
        "StartDateTime": "2013-12-11T14:25:27.3753327-05:00",
        "EndDateTime": "2013-12-11T14:25:27.3753327-05:00",
        "Description": "sample string 3",
        "Attendees": [
          {
            "EmailAddress": "sample string 1",
            "FirstName": "sample string 2",
            "LastName": "sample string 3"
          },
          {
            "EmailAddress": "sample string 1",
            "FirstName": "sample string 2",
            "LastName": "sample string 3"
          }
        ]
       },
      {
        "StartDateTime": "2013-12-11T14:25:27.3753327-05:00",
        "EndDateTime": "2013-12-11T14:25:27.3753327-05:00",
        "Description": "sample string 3",
        "Attendees": [
          {
            "EmailAddress": "sample string 1",
            "FirstName": "sample string 2",
            "LastName": "sample string 3"
          },
      ....
      ....
  {
    "Id": 1,
   "County": "sample string 16",
    "Country": "sample string 17",
    "AtAddressSinceMonth": 1,
    "AtAddressSinceYear": 1,
   "AtAddressUntilMonth": 1,
   "AtAddressUntilYear": 1,
...
...

      }
     ]
   }
  ]
我定义了一个包含所有变量的静态类Jobseker和包含所有嵌套变量的静态内部类,以及相应的set和get方法

另外,创建了另一个类ArrayOfJobseker,该类使用变量protected List Jobseker;我的类定义看起来像

public class ArrayOfJobSeeker {

protected List<ArrayOfJobSeeker.JobSeeker> jobSeeker;

public List<ArrayOfJobSeeker.JobSeeker> getJobSeeker() {
    if (jobSeeker == null) {
        jobSeeker = new ArrayList<ArrayOfJobSeeker.JobSeeker>();
    }
    return this.jobSeeker;
}



public static class JobSeeker {


    protected String address;

    protected String atAddressSinceMonth;

    protected String atAddressSinceYear;
    protected String atAddressUntilMonth;
    protected String atAddressUntilYear;
    protected String city;
    protected String country;
    protected String county;
    protected XMLGregorianCalendar dateOfBirth;
    protected ArrayOfJobSeeker.JobSeeker.Documents documents;
    protected String emailAddress;
    protected ArrayOfJobSeeker.JobSeeker.Events events;
    protected String extension;
    protected String firstName;
    protected String genderCode;
    protected ArrayOfJobSeeker.JobSeeker.HistoryActivities historyActivities;
    protected String id;
    protected String lastName;
    protected String maidenName;
    protected String middleName;
请帮忙


感谢

盲目地将任意json反序列化为强类型类结构的问题在于它太脆弱了;一个放错位置的字体和卡布姆

这是我要做的。您使用的是Java,因此在混合中加入一些groovy:

import groovy.json.JsonSlurper;
...
JsonSlurper jsonParser = new JsonSlurper();
List<Map> jsonMap = (List<Map>)jsonParser.parse(new FileReader( jsonFile));
import groovy.json.JsonSlurper;
...
JsonSlurper-jsonParser=new-JsonSlurper();
List jsonMap=(List)jsonParser.parse(newfilereader(jsonFile));
JsonSlurper将json解析为一个层次化哈希映射列表,每个哈希映射都包含数组和基本包装(Long、Boolean等);您不需要预先创建自定义类,只需解析原始json;完成了

您可以使用
jsonMap
“原样”(这就是我要做的)或者,如果您坚持创建自己的类来使用getter和setter存储所有数据,请使用调试器探索
jsonMap
,遍历对象以查看类型,那么您将确切知道要实现的任何类都需要哪些类型。与Gson相比,JsonSlurper的唯一缺点是它不会反序列化您需要自己完成这一部分


PS:如果您的json以
[]
开始/结束,
jsonParser
返回
List
,如果它以
{}
开始/结束,它返回
HashMap
作为封闭对象。

引用您在问题中的注释,您的代码真的打印了整个json字符串吗?我想您只会得到第一个元素。
如果可能的话,我建议您退出内部类,并执行以下操作

JsonArray jarray = new JsonParser().parse(json).getAsJsonArray();
for (JsonElement e : jarray) {
    JsonObject jobSeeker = e.getAsJsonObject();

    JsonArray events = e.get("Events").getAsJsonArray();
    JsonArray documents = e.get("Documents").getAsJsonArray();
    // iterate the array, deserialize the objects and do your work

    jobSeeker.remove("Events");
    jobSeeker.remove("Documents");
    // deal with the job seeker;
}

这似乎不是一个好方法,但我认为它应该有效。

错误是什么?我发现在这种情况下有效的一种方法是在内存中创建数据结构并将其封送到字符串(在本例中使用
toJson()
),保存到一个文件中,并将其与您试图解析的文本进行比较,
静态
内部类在序列化和反序列化方面与顶级类没有什么不同。我在创建基于JSON的Java类时遇到了问题,但我发现这非常有用-我不能使用内部静态类吗?请详细说明一定是格森?你考虑过其他事情吗?
import groovy.json.JsonSlurper;
...
JsonSlurper jsonParser = new JsonSlurper();
List<Map> jsonMap = (List<Map>)jsonParser.parse(new FileReader( jsonFile));
JsonArray jarray = new JsonParser().parse(json).getAsJsonArray();
for (JsonElement e : jarray) {
    JsonObject jobSeeker = e.getAsJsonObject();

    JsonArray events = e.get("Events").getAsJsonArray();
    JsonArray documents = e.get("Documents").getAsJsonArray();
    // iterate the array, deserialize the objects and do your work

    jobSeeker.remove("Events");
    jobSeeker.remove("Documents");
    // deal with the job seeker;
}