Java解析器Json作为多维数组

时间:2021-06-19 21:30:02

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

解析Json有困难。这是Json

{
  "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:

我可以用下面的代码得到“sku”值:

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?

但我无法得到“sku”值。有人能指导我如何从JSON中获得“skus”值吗?

3 个解决方案

#1


1  

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

要取回sku,你可以这样做:-

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

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

要得到这些值,你可以这样做:-

skus1 = skus.getString(0);

#2


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"));

#3


0  

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

第1点:您的示例JSON是错误的,请使用以下链接验证和格式化JSON: https://jsonformatter.curiousconcept.com/

Point 2: Use following code

要点2:使用以下代码。

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);
        }

#1


1  

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

要取回sku,你可以这样做:-

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

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

要得到这些值,你可以这样做:-

skus1 = skus.getString(0);

#2


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"));

#3


0  

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

第1点:您的示例JSON是错误的,请使用以下链接验证和格式化JSON: https://jsonformatter.curiousconcept.com/

Point 2: Use following code

要点2:使用以下代码。

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);
        }