-2

Alright. I have a JSON Object sent to me from a server which contains the following data:

{
    "result":
    [
        {"status":"green","type":"data1"},
        {"status":"green","type":"data2"},
        {"status":"green","type":"data3"}
    ],
    "status":"ok"
}

The data I want to get is the status for the three status values. Data1, data2, and data3 always show up in that order, so I'm now trying to grab the data by index (e.g. data1 = index 0, data2 = index 1, data3 = index 2). How do I do that?

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
Aneesh Ashutosh
  • 762
  • 7
  • 18

4 Answers4

1

Try following:

String stat1;
String stat2;
String stat3;
JSONObject ret; //contains the original response
//Parse to get the value
try {
    stat1 = ret.getJSONArray("results").getJSONObject(0).getString("status");
    stat2 = ret.getJSONArray("results").getJSONObject(1).getString("status");
    stat3 = ret.getJSONArray("results").getJSONObject(2).getString("status");
} catch (JSONException e1) {
    e1.printStackTrace();

}
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
1

You would use JSONObject and JSONArray, the entire string is one JSONObject so you would construct one with it.

JSONObject object = new JSONObject(YOUR_STRING_OF_JSON);

Then you can access it with different get methods depending upon your expected type.

JSONArray results = object.getJSONArray("result"); // This is the node name.
String status = object.getString("status");

for (int i = 0; i < results.length(); i++) {
    String resultStatus = results.getJSONObject(i).getString("status");
    String type = results.getJSONObject(i).getString("type");
    Log.w("JSON Result #" + i, "Status: " + resultStatus + " Type: " + type);
}

You need to surround it with a try/catch because JSON access can throw a JSONException.

Tonithy
  • 2,300
  • 1
  • 20
  • 20
  • This came in handy a little bit later in the project where there was a JSON I had to parse where I didn't know how long it would be :P Thanks!! – Aneesh Ashutosh Jul 26 '13 at 01:28
1

Try re-factoring via a forEach loop

var testData = 
{   
    "result":
    [   
        {"status":"green","type":"data1"},
        {"status":"green","type":"data2"},
        {"status":"green","type":"data3"}
    ],  
    "status":"ok"
};  


var output      = new Object;
var resultSet   = new Object;
    resultSet   = testData.result;

resultSet.forEach(function(data)
{   
    theStatus = data['status'];
    theType   = data['type']

    output[theType] = theStatus;
}); 

console.log( output['data1'] );
Gigline
  • 297
  • 1
  • 4
0

If you've got your models setup to mirror that data set, then you can let GSON (https://code.google.com/p/google-gson/) do a lot of your work for you.

If you want a bit more control, and want to parse the set yourself you can use JSONObject, JSONArray. There's an example of parsing and assembling a json string here: Android create a JSON array of JSON Objects

Community
  • 1
  • 1
jlindenbaum
  • 1,891
  • 18
  • 28
  • The issue I'm having, though, is that none of the arrays are named aside from the initial result array, so I don't know how to look inside them :/ – Aneesh Ashutosh Jul 25 '13 at 17:05
  • Based off what you've posted you know the name of the first array, the others are JSONObjects, that you can get out of the array. Once you have JSONObjects you know the name of the keys and can pull the value out as you please. – jlindenbaum Jul 25 '13 at 17:08