用Java解析嵌套的JSON

时间:2020-07-14 11:51:22

标签: java json gson

我有一个看起来像这样的奇怪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]

您能帮我解析这种模型吗?在这种情况下找不到我做错的事情。

预先感谢

3 个答案:

答案 0 :(得分:1)

要解决此问题,有两种方法:

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

答案 1 :(得分:0)

改为使用JsonReader。因此,如果您知道json以[开头,以]结尾,请使用beginArray来读取。您输入的是InputStream,可以是文件,套接字流或字符串流。

     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

答案 2 :(得分:0)

以这种方式解决的问题:

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

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

allEvents.getJSONArray(0)

然后使用杰克逊ObjectMapper的以前的方式进行映射。

谢谢!