0

I'm trying to parse this code : I need the "name" attribute under "docs" under "response".

{
  "responseHeader": {
    "status": 0,
    "QTime": 1,
    "params": {
      "q": "Lecture1-2",
      "_": "1399736575991",
      "wt": "json"
    }
  },
  "response": {
    "numFound": 6,
    "start": 0,
    "docs": [
      {
        "id": "TWINX2048-3200PRO",
        "name": "CORSAIR  XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail",
        "manu": "Corsair Microsystems Inc.",
        "manu_id_s": "corsair",
        "cat": [
          "electronics",
          "memory"
        ],
        "features": [
          "CAS latency 2,\t2-3-3-6 timing, 2.75v, unbuffered, heat-spreader"
        ]}}

I tried to do it like :

JSONObject nodeRoot  = new JSONObject(result); 
                JSONObject nodeStats = nodeRoot.getJSONObject("response");
                String sSDR = nodeStats.getString("docs");

but I'having other result...

1 Answer 1

1

"docs": [ is a JSONArray.

What you have

 String sSDR = nodeStats.getString("docs");

Change to

 JSONObject nodeStats = nodeRoot.getJSONObject("response");
 JSONArray jr = (JSONArray) nodeStats.getJSONArray("docs");
 for(int i=0;i<jr.length();i++)
 {
 JSONObject jb = jr.getJSONObject(i);
 String id = jb.getString("id");
 }
Sign up to request clarification or add additional context in comments.

2 Comments

thanks it works :) I was confused to differentiate between array and object
@MedAmini [ represents JSONArray node and { represents JSONObject node

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.