1

I want to parse an array from Server but I can't obtain the array Here is the jsonString Successfully got :

{
    "status":"OK",
    "message":"this is your start and end coordinates",
    "data":"{\"start\":[\"35.778763\",\"51.427360\"],\"end\":[\"35.768779, 51.415002\"]}"
}

I want the Double Values from data arraylist:

//try/catch
Log.d(TAG, "Passes here");          
JSONObject jObject = new JSONObject(jsonString);

JSONArray jData = jObject.getJSONArray("data");
Log.d(TAG, "Not PAASING HERE !!! ");

JSONArray jArrayStart = (JSONArray) jData.get(0);
JSONArray jArrayEnd =  (JSONArray) jData.get(1);

latitudeStart =  (Double) jArrayStart.get(0);
longtitudeStart = (Double) jArrayEnd.get(1);
latitudeEnd = (Double) jArrayEnd.get(0);
longtitudeEnd = (Double) jArrayEnd.get(1);

3 Answers 3

3

What you're trying to parse, is a string.

{
    "status": "OK",
    "message": "this is your start and end coordinates",
    "data": "{\"start\":[\"35.778763\",\"51.427360\"],\"end\":[\"35.768779, 51.415002\"]}"
}

So it works like this:

//first, retrieve the data from the response JSON Object from the server
JSONObject response = new JSONObject(jsonString);
String status = response.getString("status");
String message = response.getString("message");
//Note this: "data" is a string as well, but we'll have to parse that later.
String data = response.getString("data");

//get the doubles from the arrays from the "data" component.
JSONObject dataObject = new JSONObject(data);
JSONArray start = dataObject.getJSONArray("start");
JSONArray end = dataObject.getJSONArray("end");

for (int i = 0; i < start.length(); i++) {
    String value = start.getString(i);
    //do something with the start String (parse to double first)
}
for (int i = 0; i < end.length(); i++) {
    String value = end.getString(i);
    //do something with end String (parse to double first)
}

So data is actually a String, but represents a JSONObject (which you'll have to parse), which, in its turn, contains two JSONArrays.

If data was a JSONObject instead of a String, the JSON would have looked like this:

{
    "status": "OK",
    "message": "this is your start and end coordinates",
    "data": {
        "start": [
            "35.778763",
            "51.427360"
        ],
        "end": [
            "35.768779", //note that in your example, this quote is missing (first quote on next line too)
            "51.415002"
        ]
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, the problem was the mentioned end missing qoutation and the string type of the entries
1

The value of data is not a JSONArray its JSONObject

Explanation

JSONObject will be surrounded by {}

JSONArray will be surrounded by []

1 Comment

Still no success with the following Code : ' JSONObject jObject = new JSONObject(jsonString); JSONObject jData = jObject.getJSONObject("data"); JSONArray jArrayStart = jData.getJSONArray("start"); JSONArray jArrayEnd = jData.getJSONArray("end"); latitudeStart = (Double) jArrayStart.get(0); longtitudeStart = (Double) jArrayEnd.get(1); latitudeEnd = (Double) jArrayEnd.get(0); longtitudeEnd = (Double) jArrayEnd.get(1);'
0

data is not an array, it is a json object and therefore you can not access it the way you are doing. If you want to fetch start array from json object "data" then use below jObject.optJSONObject("data").optJSONArray("start");

same thing can be used to retrieve "end" json array.

then use optJSONObject() and/or optString() API to retrieve required value from json array.

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.