解析 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]

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

提前致谢

改为使用 JsonReader。因此,如果您知道 json 以 [ 开头并以 ] 结尾,请使用 beginArray 读取。你的in是一个InputStream,它可以是一个文件,socket流或者字符串流。

     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;

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

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

第一种方法: 将您的 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"}]}

以这种方式解决问题:

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

结果我收到了包含 2 个元素的 JSONArray。然后通过

提取需要的一个
allEvents.getJSONArray(0)

然后使用 jackson ObjectMapper 以之前的方式进行映射。

感谢回复!