firstTimeJavaUser
firstTimeJavaUser

Reputation: 25

parse JSON to grab 'name' from an array

I'm trying to grab the 'name' from the JSON snippet I've included. I have tried the following but what I'm expecting is never grabbed.

Edit: 'output' is the full JSON string in case it wasn't already understood ;)

JSONObject result = null;
JSONArray data = null;
try {
    try {result = new JSONObject(output);} catch (JSONException e) {e.printStackTrace();}

try {
    data = result.getJSONArray("data");

                for(int a=0;a<data.length();a++){
                    System.out.println(result.getJSONObject(String.valueOf(a)).getString("name"));//getJSONObject("results")
                }

Here is the JSON snippet I'm trying to work with:

{
    "code": 200,
    "status": "Ok",
    "copyright": "© 2015 MARVEL",
    "attributionText": "Data provided by Marvel. © 2015 MARVEL",
    "attributionHTML": "<a href=\"http://marvel.com\">Data provided by Marvel. © 2015 MARVEL</a>",
    "etag": "b130a8b7af591e4e7ca078753f9c5c8a76e55e5d",
    "data": {
        "offset": 0,
        "limit": 20,
        "total": 1485,
        "count": 20,
        "results": [
            {
                "id": 1011334,
                "name": "3-D Man",
                "description": "",
                "modified": "2014
                .
                .
                .
                .
                .
                .
                .
                .
                .
                .

Upvotes: 1

Views: 141

Answers (3)

Anjali Tripathi
Anjali Tripathi

Reputation: 1477

You can parse like that

 JsonObject obj = new JsonObject(StringResponse);

    String code  = obj.getString(code);
    //same way you can get other string

JsonObject obj1 = obj.getJsonObject(Data);
    String offset= obj.getString(offset);
    //same way you can get other string

JsonArray jsonarr = obj1.getJsonArray(results);

for(i=0;i< jsonarr.size(); i++){
JsonObject innerObj = jsorr.getJsonObject(i);

  String id= obj.getString(id);
    //same way you can get other string


}

Hope It will helpful for you

Upvotes: 0

windpush
windpush

Reputation: 9

 JSONTokener jsonTokener = new JSONTokener(jsonVaule);
  JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();
  Int code =jsonObject.getInt("code");
  String status =jsonObject.getString("status");
  //{obejcet,obejcet,...}

  for data{} is the same way~
  JSONObject dataJsObject = (JSONObject)  jsonObject.getJsonObject("data");
  int offset =dataJsObject.getInt(""iffset);
  ....
  //[{},{}] this type is jsonArrary
  JSONArray results = dataJsObject.getJSONArray("results"); 
   for(JSONObject resultJsonObj:JSONArray){
      Int id =jsonObject.getInt("id");
      //...and so on
   }

hope it can help you~

Upvotes: 0

yts
yts

Reputation: 1890

To get you started, "data" points to a JSON Object, not array. So it should be:

data = result.getJSONObject("data");

Then, "results" points to a JSON array:

JSONArray results = data.getJSONArray("results"); 

Then you can try your loop. You shouldn't be turning a into a String - getJSONObject() takes an int for the index.

In case you're confused between Objects and Arrays, a JSON object has key - value pairs and are enclosed in curly braces. The keys are strings and the values can be a mix of any type:

{"key1": 5, "key2": "value2", "key3": {
    "anotherObject": [1,2,3,4]
    }
}

An array is a list of objects and is enclosed in square brackets:

[{...}, {...}, {...}]

The elements in the list don't have to be JSON objects, and in good JSON they will all be of the same type:

[1,2,3,4,4] or ["we", "are", "in", "an", "array"]

Upvotes: 2

Related Questions