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?

asked Feb 8, 2012 at 11:07
1
  • p-xr.com/… Commented Feb 8, 2012 at 11:13

4 Answers 4

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));
 }
answered Feb 8, 2012 at 11:18
Sign up to request clarification or add additional context in comments.

1 Comment

let me know if u have any doubt
0

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

answered Feb 8, 2012 at 11:12

Comments

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

answered Feb 8, 2012 at 11:19

Comments

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");
}
answered Feb 8, 2012 at 11:24

1 Comment

I had misspelling on my code, it should be jsonplace.getString("placeName"); instead of jsonplaces.getString("placeName");

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.