ho la seguente stringa passata al server:GSON: non è un oggetto JSON
{
"productId": "",
"sellPrice": "",
"buyPrice": "",
"quantity": "",
"bodies": [
{
"productId": "1",
"sellPrice": "5",
"buyPrice": "2",
"quantity": "5"
},
{
"productId": "2",
"sellPrice": "3",
"buyPrice": "1",
"quantity": "1"
}
]
}
che è un JSON valido per http://jsonlint.com/
voglio ottenere il campo corpi array.
Ecco come lo sto facendo:
Gson gson = new Gson();
JsonObject object = gson.toJsonTree(value).getAsJsonObject();
JsonArray jsonBodies = object.get("bodies").getAsJsonArray();
Ma sulla seconda riga mi sto eccezione elencati di seguito:
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\"}]}"
come farlo correttamente allora?
Potrebbe voler dare un'occhiata a questo http://stackoverflow.com/a/15116323/2044733. La seconda opzione, che inizia "Per usare JsonObject", sembra esattamente quello che vuoi. – bbill