简体   繁体   English

使用GSON将String解析为JsonObject会产生IllegalStateException:这不是JSON对象

[英]Parsing String to JsonObject using GSON gives IllegalStateException: This is not a JSON Object

I have the following code: 我有以下代码:

JsonParser parser = new JsonParser();
System.out.println("gson.toJson: "  + gson.toJson(roomList));
JsonObject json2 = parser.parse("{\"b\":\"c\"}").getAsJsonObject();
System.out.println("json2: " + json2);
JsonObject json = parser.parse(gson.toJson(roomList)).getAsJsonObject();
System.out.println("json: " + json);

It gives me the following output: 它给了我以下输出:

gson.toJson: [{"id":"8a3d16bb328c9ba201328c9ba5db0000","roomID":9411,"numberOfUsers":4,"roomType":"BigTwo"},{"id":"402881e4328b9f3a01328b9f3bb80000","roomID":1309,"numberOfUsers":4,"roomType":"BigTwo"},{"id":"402881e4328ba09101328ba09edd0000","roomID":1304,"numberOfUsers":4,"roomType":"BigTwo"},{"id":"402881e4328bb83601328bb8af640000","roomID":4383,"numberOfUsers":4,"roomType":"BigTwo"},{"id":"402881e4328bb83601328bd271fe0001","roomID":5000,"numberOfUsers":4,"roomType":"BigTwo"},{"id":"402881e4328bb83601328bd2e0e30002","roomID":2485,"numberOfUsers":4,"roomType":"BigTwo"},{"id":"402881e4328bb83601328bd3087b0003","roomID":6175,"numberOfUsers":4,"roomType":"BigTwo"},{"id":"402881e4328bb83601328bd35a840004","roomID":3750,"numberOfUsers":4,"roomType":"BigTwo"},{"id":"402881e4328bb83601328bd366250005","roomID":370,"numberOfUsers":4,"roomType":"BigTwo"},{"id":"402881e4328bb83601328bd3807d0006","roomID":9477,"numberOfUsers":4,"roomType":"BigTwo"}]
json2: {"b":"c"}
java.lang.IllegalStateException: This is not a JSON Object.

Can someone please help me parse my Json string to JsonObject? 有人可以帮我解析我的Json字符串到JsonObject吗? I have checked in http://jsonlint.com/ that my json is valid though. 我已经在http://jsonlint.com/检查过我的json是有效的。

It's because due to the JSON structure.. I have to put it into a JSONObject first, like so 这是因为由于JSON结构......我必须首先将它放入JSONObject中,就像这样

JsonObject jsonObj = new JsonObject();
jsonObj.addProperty(ServerConstants.JSONoutput, gson.toJson(roomList));

Then I would deserialize like 然后我会反序列化

 List<RoomData> roomList = gson.fromJson(jsonObj.get(CardGameConstants.JSONoutput).toString(), listType);
            for (RoomData roomData : roomList) {
                System.out.println(roomData.getRoomID());
            }

The problem with the code is that roomList is not a JsonObject as evident in the printed output. 代码的问题是roomList不是JsonObject ,在打印输出中很明显。 It's actually a JsonArray . 它实际上是一个JsonArray

To fix the code, call .getAsJsonArray() (instead of .getAsJsonObject() ) 要修复代码,请调用.getAsJsonArray() (而不是.getAsJsonObject()

JsonParser parser = new JsonParser();
System.out.println("gson.toJson: "  + gson.toJson(roomList));
JsonObject json2 = parser.parse("{\"b\":\"c\"}").getAsJsonObject();
System.out.println("json2: " + json2);
JsonArray json = parser.parse(gson.toJson(roomList)).getAsJsonArray();
System.out.println("json: " + json);

This should give you some basic idea. 这应该给你一些基本的想法。

ArrayList<String> roomTypes = new ArrayList<String>();

JSONArray jsonArray = new JSONArray(jsonString);

for(int i =0; i<jsonArray.length(); i++){
    roomTypes.add(jsonArray.getJSONObject(i).getString("roomType"));
}

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

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