0

this is the response from the server

{"route":[1,2,3,4,5,6]}

This is my code:

try{
String d = json.getString("route");
}
}catch(JSONException je){
}

and im getting NullPointerException. please help me.

This is my Server Response, Now Give me the solution Link -> http://ajax.tpksym.cloudbees.net/route/route14

5
  • Can you please show us more code snippet. Commented May 2, 2014 at 11:17
  • the answer is given please have a look, fully pared and tested, add try/catch block with yourself Commented May 2, 2014 at 11:22
  • link This is the response from my server. Commented May 2, 2014 at 11:30
  • hi than please choose double in place of int in the answer Commented May 2, 2014 at 11:44
  • The answer is again updated please have a look Commented May 2, 2014 at 11:48

3 Answers 3

1

It should be like:

JSONObject jsonResult = new JSONObject("{\"route\":[1,2,3,4,5,6]}");
JSONArray array = jsonResult.getJSONArray("route");
for (int i = 0; i < array.length(); i++) {
     int data = array.getInt(i);
}
....
Sign up to request clarification or add additional context in comments.

Comments

1
try {
        String jsonString = "{\"route\":[1,2,3,4,5,6]}";
        JSONObject jsonObject = new JSONObject(jsonString);
        JSONArray jsonArray = jsonObject.getJSONArray("route");
        for (int i = 0; i < jsonArray.length(); i++) {
           System.out.println(jsonArray.getInt(i));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

Comments

1

Hi as yours json is as follows

{
  "route": [
    1,
    2,
    3,
    4,
    5,
    6
  ]
}

so do as follows

String jsondata = "{\"route\":[1,2,3,4,5,6]}";
        JSONObject primaryObject = new JSONObject(jsondata);

        JSONArray jarray = primaryObject.getJSONArray("route");

        for (int i = 0; i < jarray.length(); i++) {

            Integer data = jarray.getInt(i);
            System.out.println("data=="+data);
        }

as you gave the link http://ajax.tpksym.cloudbees.net/route/route14

and data seems there coming as in double i.e. 13.56 etc

so use as follows

String jsondata = "JSON DATA FROM SERVER";
        JSONObject primaryObject = new JSONObject(jsondata);

        JSONArray jarray = primaryObject.getJSONArray("route");

        for (int i = 0; i < jarray.length(); i++) {

            Double data = jarray.getDouble(i);

        }

3 Comments

sorry i'm getting NullPointerException :(
where ? can you send the log? if double is having a problem that treat it as string and later you can conver it to double or else
i do not think nullpointer exception is in parsing as if the exception will be there than it will be of type JSONException not nullpointer, please check log, have a debug and than solve

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.