【发布时间】:2015-08-23 09:27:01
【问题描述】:
我将以下字符串传递给服务器:
{
"productId": "",
"sellPrice": "",
"buyPrice": "",
"quantity": "",
"bodies": [
{
"productId": "1",
"sellPrice": "5",
"buyPrice": "2",
"quantity": "5"
},
{
"productId": "2",
"sellPrice": "3",
"buyPrice": "1",
"quantity": "1"
}
]
}
这是http://jsonlint.com/的有效json
我想获取bodys数组字段。
我就是这样做的:
Gson gson = new Gson();
JsonObject object = gson.toJsonTree(value).getAsJsonObject();
JsonArray jsonBodies = object.get("bodies").getAsJsonArray();
但在第二行,我得到了下面列出的异常:
HTTP Status 500 - Not a JSON Object: "{\"productId\":\"\",\"sellPrice\":\"\",\"buyPrice\":\"\",\"quantity\":\"\",\"bodies\":[{\"productId\":\"1\",\"sellPrice\":\"5\",\"buyPrice\":\"2\",\"quantity\":\"5\"},{\"productId\":\"2\",\"sellPrice\":\"3\",\"buyPrice\":\"1\",\"quantity\":\"1\"}]}"
那该怎么做呢?
【问题讨论】:
-
可能想看看这个stackoverflow.com/a/15116323/2044733。第二个选项,以“To use JsonObject”开头,看起来正是您想要的。