Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 1.52 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android: Decoding JSON
  2. String s = "[{"placeID":"p0001","placeName":"INTI International University","placeType":"Education","
  3.             + ""placeLat":"2.813997","placeLng":"101.758229","placePict":""},"
  4.             + "{"placeID":"p0002","placeName":"Nilai International College","placeType":"Education","placeLat":"2.814179","placeLng":"101.7700107","placePict":""}]";
  5.     ArrayList<String> arrplaceID = new ArrayList<String>();
  6.     ArrayList<String> arrplaceName = new ArrayList<String>();
  7.     try {
  8.         JSONArray arr = new JSONArray(s);
  9.         for (int i = 0; i < arr.length(); i++) {
  10.             JSONObject jsonObject = arr.getJSONObject(i);
  11.             arrplaceID.add(jsonObject.optString("placeID"));
  12.             arrplaceName.add(jsonObject.optString("placeName"));
  13.         }
  14.     } catch (JSONException e) {
  15.         // TODO Auto-generated catch block
  16.         e.printStackTrace();
  17.     }
  18.  
  19.     for (int i = 0; i < arrplaceID.size(); i++) {
  20.         Log.e("arr[" + i + "] place Name", arrplaceName.get(i));
  21.     }
  22.        
  23. JSonArray jArray = new JsonArray(responseString);
  24.        
  25. for(i,i<jArray.size,i++)
  26.    {
  27.       JsonObject obj=jArray.get(i);
  28.      // here you got your first entry in jsonObject.
  29.      // nor use this obj according to ur need. you can say obj.getString("placeID");
  30.     // and so on.
  31.      }
  32.        
  33. JSONArray jsonplaces = new JSONObject(stringPlaces);
  34.        
  35. for (int i = 0; i < jsonplaces.length(); i++) {
  36. JSONObject jsonplace = (JSONObject) jsonplaces.get(i);
  37. //read items, for example:
  38. String placeName = jsonplace.getString("placeName");
  39. }