1

here is my code..in android String

result[{"Submitted_Date_Time":"12\/3\/2012","City":"","Issue_Category":"Graffity","OnProgress_Date_Time":"","id":"000000000000000","area":"SH 55","State":"  ","Issue_Description":" ","Closed_Date_Time":"","imagepath":"android.graphics.Bitmap@40d55cd8","Latitude":"23.71","Longitude":"72.04","Issue_Status":"Closed","LandMark":" "}] 

and i try to convert it in to json array and when i want to retrive json object from json array it gives me nullpointer exception...

JSONArray jarray = new JSONArray(result);
JSONObject jobj = jarray.getJSONObject(1);

plz help me.. thnkx in advance..

0

3 Answers 3

1

first your json String if it's contain "result" then it is not valid you can check it here http://jsonviewer.stack.hu/

to make valid json string just use String.replace as:

String finaljson=result.replace("result", "");

now JSON String is valid you can parse it as:

JSONArray jsonarray = new JSONArray(finaljson);
for (int i = 0 ; i < jarray.length() ; i++) {
 JSONObject jsonobj = jarray.getJSONObject(i);
  // get value from json object here 
    String str_City=jsonobj.getString("City");
    ///....
}
Sign up to request clarification or add additional context in comments.

Comments

0

The Json array contains only one entry, the index is 0

JSONArray jarray = new JSONArray(result);
JSONObject jobj = jarray.getJSONObject(0);

To handle it right, access the json array using a for loop

JSONArray jarray = new JSONArray(result);
for (int i = 0 ; i < jarray.length() ; i++) {
     JSONObject jobj = jarray.getJSONObject(i);
     .....
}

Comments

0

Update your below code line it will solve your problem.

JSONObject jobj = jarray.getJSONObject(0);

or you can write below code instead of your above code line if object is more than one.

for(int i=0;i<jarray.length();i++){
    JSONObject jobj = jarray.getJSONObject(i);
}

Comments

Your Answer

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.