I have JSON code like this:
[{ "idShipping":"1328448569",
"shippingDesti":"nusa tenggara barat",
"shippingCosts":"21000"
},
{ "idShipping":"1328448543",
"shippingDesti":"nusa tenggara timur",
"shippingCosts":"76000"
}]
I followed a tutorial from this link: BlackBerry read json string from an URL . I changed
private static final String NAME = "name";
from DataParser.java into
private static final String NAME = "idShipping";
but when i run it on a simulator, it showed a popup screen that said that it failed to parse data from MyScreen.java. It means I can get the JSON string, but I can't parse it.
How do I fix it?
For the JSON you've shown, you would parse the idShipping
values something like this:
//String response = "[{\"idShipping\":\"1328448569\",\"shippingDesti\":\"nusa tenggara barat\",\"shippingCosts\":\"21000\"},{\"idShipping\":\"1328448543\",\"shippingDesti\":\"nusa tenggara timur\",\"shippingCosts\":\"76000\"}]";
try {
JSONArray responseArray = new JSONArray(response);
for (int i = 0; i < responseArray.length(); i++) {
JSONObject nextObject = responseArray.getJSONObject(i);
if (nextObject.has("idShipping")) {
String value = nextObject.getString("idShipping");
System.out.println("next id is " + value);
}
}
} catch (JSONException e) {
// TODO: handle parsing error here
}
The key, as Signare said, was parsing a JSONArray
, and then getting a string value out of that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.