Old JSON:
{
"listing" : [
{
"id" : "l101",
"name" : "Paul"
}
]
}
and Code to parse JSON,
private static final String TAG_LISTING = "listing";
JSONArray contacts = null;
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
try {
contacts = json.getJSONArray(TAG_LISTING);
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
}
}
But my new JSON looks like below:
[
{
"id" : "l101",
"name" : "Paul"
}
]
so my question, where i need to make changes in my code, to parse new JSON ?
首先在 Here验证您的 json。
You should take a look to this tutorial: http://www.mkyong.com/java/json-simple-example-read-and-write-json/
Basically first you have to have a JSONObject and then you have to associate you JSONArray to a "name tag".
After correcting your JSON parse it like this:
private static final String TAG_LISTING = "listing";
JSONArray contacts = null;
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONArray json = jParser.getJSONFromUrl(url);
try {
for(int i = 0; i < json.length(); i++){
JSONObject c = json.getJSONObject(i);
}
}
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.