2

I have some troubles parsing Json. This is the Json that I have:

{
  "responseHeader": {
    "status": 0,
    "QTime": 1
  },
  "response": {
    "numFound": 3447,
    "start": 0,
    "docs": [
      {
        "sku": "5848-05855-0064",
        "skus": [
          "5848-05855-0064",
          "5848-05855-0064-5340"
        ]
      },
      {
        "sku": "5848-05849-0059",
        "skus": [
          "5848-05849-0059",
          "5848-05849-0059-5340"
        ]
      }
    ]
  },
  "facet_counts": {
    "facet_queries": {},
    "facet_fields": {},
    "facet_dates": {},
    "facet_ranges": {}
  }
}
    ]
  },
  "facet_counts": {
    "facet_queries": {},
    "facet_fields": {},
    "facet_dates": {},
    "facet_ranges": {}
  }
}

I could get "sku" value with the below code:

String jsonData = responses.body().string();

JSONObject Jobject = new JSONObject(jsonData);      
Jobject = (JSONObject) Jobject.get("response");     
Object skuValues = Jobject.get("docs");

JSONArray jArray = (JSONArray) skuValues;       
JSONObject skuConfig = jArray.getJSONObject(1);
String sku = skuConfig.getString("sku");

But I couldn't get the "skus" values. Can someone guide me on how I can get "skus" value from the JSON?

1

3 Answers 3

1

To retrieve the skus, you can do some thing like this:-

JSONArray skus = skuConfig.getJSONArray("skus");

and to get the values you can do something like this:-

skus1 = skus.getString(0);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! Worked for me!!
0

Following worked for me:

Gson gson = new Gson();
Map fromJson = gson.fromJson("{  \"responseHeader\": {    \"status\": 0,    \"QTime\": 1  },  \"response\": {    \"numFound\": 3447,    \"start\": 0,    \"docs\": [      {        \"sku\": \"5848-05855-0064\",        \"skus\": [          \"5848-05855-0064\",          \"5848-05855-0064-5340\"        ]      },      {        \"sku\": \"5848-05849-0059\",        \"skus\": [          \"5848-05849-0059\",          \"5848-05849-0059-5340\"        ]      }    ]  },  \"facet_counts\": {    \"facet_queries\": {},    \"facet_fields\": {},    \"facet_dates\": {},    \"facet_ranges\": {}  }}", Map.class);
JSONObject Jobject = new JSONObject(fromJson);      
StringMap Jobject1 = (StringMap) Jobject.get("response");     
Object skuValues = Jobject1.get("docs");

ArrayList jArray = (ArrayList) skuValues;       
StringMap skuConfig = (StringMap) jArray.get(0);
System.out.println(skuConfig);
System.out.println(skuConfig.get("skus"));

Comments

0

Point 1: Your Sample JSON is wrong, use the following link to verify and format JSON https://jsonformatter.curiousconcept.com/

Point 2: Use following code

JSONObject Jobject;
        Jobject = new JSONObject(jsonData);
        Jobject = (JSONObject) Jobject.get("response");
        JSONArray docs = Jobject.getJSONArray("docs");

        for (int i = 0; i < docs.length(); i++) {
            JSONObject skuDoc = docs.getJSONObject(i);
            String skuValue = skuDoc.optString("sku");
            JSONArray skusArr = skuDoc.getJSONArray("skus");
            System.out.println(skusArr);
        }

Comments

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.