繁体   English   中英

解析嵌套在 Java 中的 JSON

[英]Parsing nested JSON in Java

我有一个奇怪的 JSON 看起来像这样

[
  [
    {
      "id": "1",
      "clientId": "user"
    },
    {
      "id": "2",
      "clientId": "user"
    } 
 ],
  [
    {
      "Status": "NotCompleted",
      "StatusId": 0
    },
    {
      "Status": "Importing",
      "StatusId": 10
    }
  ]
]

我正在尝试用 Gson 或 JsonParser 解析它。

类看起来像这样

public class Event {
    public String id;
    public String clientId;
}

public class Status {
    public String Status;
    public String StatusId;
}

public class AllEvents {

public Event[] events;
public Status[] statuses;
}

但是当我试图用 Gson 解析它时(例如)

  AllEvents[] r = new Gson().fromJson(response, AllEvents[].class);

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 6 path $[0]

你能帮我解析这种model吗? 在这种情况下找不到我做错了什么。

提前致谢

要解决这样的问题,有两种方法:

第一种方法:将您的 JSON 文件内容(allEvents)更改为:

[
  {
    "events": [
      {
        "id": "1",
        "clientId": "user"
      },
      {
        "id": "2",
        "clientId": "user"
      }
    ],
    "statuses": [
      {
        "Status": "NotCompleted",
        "StatusId": 0
      },
      {
        "Status": "Importing",
        "StatusId": 10
      }
    ]
  }
]

之后,您的代码将完美运行。

第二种方法:

您需要根据上面的 JSON 结构进行编码:

请在下面找到对您有帮助的代码。

    Gson gson = new Gson();
    Object[] r = gson.fromJson(loadDataAsString(), Object[].class);
    AllEvents allEvents = new AllEvents();
    //if your json structure position is fixed the do this commented code
    //allEvents.events = gson.fromJson(gson.toJson(r[0]), Event[].class); //if your json Event structure position is fixed at 0 index
    //allEvents.statuses = gson.fromJson(gson.toJson(r[1]), Status[].class); //if your json Status structure position is fixed at 1 index
    //if your json structure position is not fixed the do below code
    allEvents.events = Arrays.stream(r)
            .flatMap(x -> Arrays.stream(gson.fromJson(gson.toJson(x), Event[].class)))
            .filter(y -> y.id != null).toArray(Event[]::new);//id as primary key
    allEvents.statuses = Arrays.stream(r)
            .flatMap(x -> Arrays.stream(gson.fromJson(gson.toJson(x), Status[].class)))
            .filter(y -> y.Status != null).toArray(Status[]::new);//Status as primary key
    System.out.println(gson.toJson(allEvents));//{"events":[{"id":"1","clientId":"user"},{"id":"2","clientId":"user"}],"statuses":[{"Status":"NotCompleted","StatusId":"0.0"},{"Status":"Importing","StatusId":"10.0"}]}

请改用 JsonReader。 因此,如果您知道 json 以 [ 开头并以 ] 结尾,请使用 read 和 beginArray。 你的 in 是一个 InputStream,它可以是一个文件、套接字 stream 或字符串 stream。

     JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
     List<YourMessage> messages = new ArrayList<YourMessage>();

     reader.beginArray();
     while (reader.hasNext()) {
        messages.add (do something with reader);//<-- that is pseudo code
     }
     reader.endArray();
     return messages;

有关更多详细信息,请查看此链接https://www.javadoc.io/doc/com.google.code.gson/gson/2.8.0/com/google/gson/stream/JsonReader.html

以这种方式解决的问题:

org.json.JSONArray allEvents = new org.json.JSONArray(response.getBodyAsString());

结果,我收到了带有 2 个元素的 JSONArray。 然后通过提取需要的一个

allEvents.getJSONArray(0)

然后用jackson ObjectMapper按前面的方式映射。

谢谢回复!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM