Tôi đã String sau truyền đến máy chủ:Gson: Không phải là một đối tượng JSON
{
"productId": "",
"sellPrice": "",
"buyPrice": "",
"quantity": "",
"bodies": [
{
"productId": "1",
"sellPrice": "5",
"buyPrice": "2",
"quantity": "5"
},
{
"productId": "2",
"sellPrice": "3",
"buyPrice": "1",
"quantity": "1"
}
]
}
mà là một json hợp lệ cho http://jsonlint.com/
Tôi muốn để có được những lĩnh vực cơ quan mảng.
Đó là cách tôi đang làm nó:
Gson gson = new Gson();
JsonObject object = gson.toJsonTree(value).getAsJsonObject();
JsonArray jsonBodies = object.get("bodies").getAsJsonArray();
Nhưng trên dòng thứ hai tôi nhận được ngoại lệ được liệt kê dưới đây:
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\"}]}"
Làm thế nào để làm điều đó đúng rồi?
Có thể muốn xem http://stackoverflow.com/a/15116323/2044733 này. Tùy chọn thứ hai, bắt đầu "Để sử dụng JsonObject," trông giống như chính xác những gì bạn muốn. – bbill