I have the following JSON-String:
{"locale":"de","strings":[{"key":"navigation.search","value":"Suchen"}]}
I try to parse it this way:
// parse the response text into JSON
String json = response.getText();
Window.alert(json);
JSONValue jsonValue = JSONParser.parseLenient("{\"locale\":\"de\",\"strings\":[{\"key\":\"navigation.search\",\"value\":\"Suchen\"}]}");
Window.alert(jsonValue.isObject().get("locale").toS开发者_开发技巧tring());
JSONArray jsonArray = jsonValue.isObject().get("strings").isArray();
for (int i = 0; i < jsonArray.size(); i++) {
Window.alert(jsonArray.get(i).isObject().get("key").isString() + " -> " + jsonArray.get(i).isObject().get("value").isString());
}
Why are there quotes in the Window.alert(), I thought, the quotes are essential, for creating JSON.
By the way, the JSON is created with the json.org Java-Classes:
PrintWriter w = new PrintWriter(System.err);
new JSONWriter(w)
.object()
.key("locale").value("de").key("strings").array().object().key("key").value("navigation.search").key("value").value("Suchen").endObject().endArray()
.endObject();
w.flush();
I think the key is to use the stringValue method. Try:
jsonArray.get(i).isObject().get("value").isString().stringValue()
精彩评论