0

[{"placeID":"p0001","placeName":"INTI International University","placeType":"Education","placeLat":"2.813997","placeLng":"101.758229","placePict":""},{"placeID":"p0002","placeName":"Nilai International College","placeType":"Education","placeLat":"2.814179","placeLng":"101.7700107","placePict":""}]

How do I decode the JSON sent from my PHP script on Android?

DroidMatt
  • 183
  • 1
  • 4
  • 20
  • http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/ – Andy Feb 08 '12 at 11:13

4 Answers4

1

please try this

 String s = "[{\"placeID\":\"p0001\",\"placeName\":\"INTI International University\",\"placeType\":\"Education\","
            + "\"placeLat\":\"2.813997\",\"placeLng\":\"101.758229\",\"placePict\":\"\"},"
            + "{\"placeID\":\"p0002\",\"placeName\":\"Nilai International College\",\"placeType\":\"Education\",\"placeLat\":\"2.814179\",\"placeLng\":\"101.7700107\",\"placePict\":\"\"}]";
    ArrayList<String> arrplaceID = new ArrayList<String>();
    ArrayList<String> arrplaceName = new ArrayList<String>();
    try {
        JSONArray arr = new JSONArray(s);
        for (int i = 0; i < arr.length(); i++) {
            JSONObject jsonObject = arr.getJSONObject(i);
            arrplaceID.add(jsonObject.optString("placeID"));
            arrplaceName.add(jsonObject.optString("placeName"));
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    for (int i = 0; i < arrplaceID.size(); i++) {
        Log.e("arr[" + i + "] place Name", arrplaceName.get(i));
    }
Ramesh Solanki
  • 2,961
  • 4
  • 28
  • 44
0

What is the problem in this Please Read this tutorial for parsing JSON it might be helpful in future also.json parsing link

deepak Sharma
  • 1,641
  • 10
  • 23
0

Follow below points.

1) it seems the response you are getting is Json Array. so create one json array by response string.

              JSonArray jArray = new JsonArray(responseString);

2) now you have your response in jArray. now iterate a loop and take json object from JsonArray, in your case you have two json objects.

      for(i,i<jArray.size,i++)
   {
      JsonObject obj=jArray.get(i);
     // here you got your first entry in jsonObject. 
     // nor use this obj according to ur need. you can say obj.getString("placeID");
    // and so on.
     }

refer this to understand more on json link

AAnkit
  • 27,299
  • 12
  • 60
  • 71
0

use JSONArray class:

JSONArray jsonplaces = new JSONObject(stringPlaces);

then your able to iterate throught array by using for-loop:

for (int i = 0; i < jsonplaces.length(); i++) {
    JSONObject jsonplace = (JSONObject) jsonplaces.get(i);
    //read items, for example:
    String placeName = jsonplace.getString("placeName");
}
Veikko Karsikko
  • 3,671
  • 1
  • 20
  • 15