{"totalCount":2,The big different about JSON and XML is that JSON is a subset of JavaScript. I can use JavaScript's own compiler to do just that by calling eval. Parsing JSON is a one-liner! Moreover, navigating an object synthesized from JSON is identical to navigating any JavaScript object. It's far easier than navigating through the DOM tree.
"results":[{"bh":"100","sm":"hello"},{"bh":"101","sm":"me" }]}
To create a JSON object in server side with Java. We can use the lib in "json.org". It provides a quick API to parse POJO and list etc. See the following example code:
public JSONObject toJSONObject() throws Exception {To parse JSON data from JS in serve side, please see the following example:
JSONObject json = new JSONObject();
json.put("totalCount", totalCount);
JSONArray jsonItems = new JSONArray();
for (Iteratoriter = results.iterator(); iter.hasNext();) {
jsonItems.put(iter.next().toJSONObject());
}
json.put("results", jsonItems);
return json;
}JSONArray jsonArray = JsonUtil.getJsonArray(jsonString);
// JsonUtil.getJsonArray(jsonString) just do this ->
// jsonArray = new JSONArray(jsonString);
// loop through - get from json and update
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String id = jsonObject.getString("id");
}
2 comments:
Hello there,
I was wondering if you have the code for JsonUtil.getJsonArray(jsonString)...I mean this class? I have not been able to find anything.
right now I am parsing the String into an object and converting the same to JSONArray. I know only two lines, but this class that you have used would give me better results.
Thanks,
Manju
Hi, Manju:
Would you please give me your email address. My email is: ffzhuang@gmail.com
Post a Comment