Praveen Thamizhazhagan
Praveen Thamizhazhagan

Reputation: 841

getting ' JSON EXCEPTION: no value for ' error?

i'm trying to parse a json array "itemList":

 {
    "itemsCount": "1309",
    "itemList": [
        { name: "xxx",
          id: "01"
        },
        { name: "yyy",
          id: "02"
        }
      ]
    }

but i got " json exception:no value for " when i use the following code.

 String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET", params);
Log.d("Albums JSON: ", "> " + json);                    
try {   
     jsonObject = new JSONObject().getJSONObject("itemList");
     albums = jsonObject.getJSONArray("itemList"); 
         if (albums != null) {

    for (int i = 0; i < albums.length(); i++) {
    JSONObject c = albums.getJSONObject(i);

    String id = c.getString(TAG_ID);
    String name = c.getString(TAG_NAME);


    HashMap<String, String> map = new HashMap<String, String>();

    map.put(TAG_ID, id);
        map.put(TAG_NAME, name);


    albumsList.add(map);
                }
            }else{
                Log.d("Albums: ", "null");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

in the log i'm able to see the json values. i'm getting error in following lines.

jsonObject = new JSONObject().getJSONObject("itemList");
 albums = jsonObject.getJSONArray("itemList"); 

i'm new to this .help me solve it. Thanks in advance

Upvotes: 1

Views: 2522

Answers (3)

Vivart
Vivart

Reputation: 15303

You need to send the json string for creating a jsong object.

jsonObject = new JSONObject(json);
 albums = jsonObject.getJSONArray("itemList"); 

Upvotes: 1

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

try this way

 jsonObject = new JSONObject(json);

Upvotes: 0

Nicklas Gnejs Eriksson
Nicklas Gnejs Eriksson

Reputation: 3415

If this task if not a educational/academic one I would recommend you to use a JSON Mapper. For example Jackson or GSON. "Manually" parsing JSON like this is just a waste of time and code and makes it much more error prone.

http://jackson.codehaus.org/

Doing the same things with a mapper would be two lines of code, literally.

Upvotes: 0

Related Questions