Hosein Hamedi
Hosein Hamedi

Reputation: 322

Parse Json array without keys

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);

Upvotes: 1

Views: 10469

Answers (3)

stealthjong
stealthjong

Reputation: 11093

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"
        ]
    }
}

Upvotes: 3

TheGraduateGuy
TheGraduateGuy

Reputation: 1520

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.

Upvotes: 0

bhargavg
bhargavg

Reputation: 1379

The value of data is not a JSONArray its JSONObject

Explanation

JSONObject will be surrounded by {}

JSONArray will be surrounded by []

Upvotes: 1

Related Questions