从java中的Json Object获取值

时间:2018-05-26 09:49:52

标签: java json

"response": {
"code": "0",
"comment": "",
"log_id": "51c55b29-e1e7-4855-9fd8-d920eff82fbe"},
"styles": {
"style": [{
  "style_id": "3745150",
  "style_name": "Westy",
  "style_number": "WEST01M1",
  "style_identifier": "WEST01M1||",
  "style_description": "",
  "made_to_order": "True",
  "division": "MEN'S SHOES",
  "division_code": "M1",
  "style_country_origin": "",
  "measurements": "",
  "fabrication": "",
  "fabrication_code": "",
  "silhouette": "",
  "silhouette_code": "",
  "source_of_materials": "",
  "heel_height": "",
  "contains_fur": "",
  "best_seller": "False",
  "minimum": "12",

  "categories": [  
    {
        "category_parent": "Men's",
        "subcategories": [{
            "subcategory_parent": "Shoes",
            "subcategory": [
                {
                    "subcategory_id": "840",
                    "subcategory_label": "Casual"
                }
            ]}
        ]} 
 ],
  "prices": [
      {
        "price_label": "",
        "price_currency": "USD",
        "price_wholesale": 30.00,
        "price_retail": 75.00,
        "price_currency_retail": "USD"
      }
  ],
  "colors": [{
        "color_name": "Black",
        "color_code": "1",
        "color_image": "",
        "color_swatch": "/img/uploads/accounts/280561/images/WEST01M1 (1).jpg",
        "minimum": "0"
      }]
  ,
  "images": [
      "/img/uploads/accounts/280561/images/WEST01M1.jpg"
  ]
}, {
  "style_id": "3745126",
  "style_name": "Clarity",
  "style_number": "CLAR01M1",
  "style_identifier": "CLAR01M1||",
  "style_description": "",
  "made_to_order": "True",
  "division": "MEN'S SHOES",
  "division_code": "M1",
  "style_country_origin": "",
  "measurements": "",
  "fabrication": "",
  "fabrication_code": "",
  "silhouette": "",
  "silhouette_code": "",
  "source_of_materials": "",
  "heel_height": "",
  "contains_fur": "",
  "best_seller": "False",
  "minimum": "12",

  "categories": [  
    {
        "category_parent": "Men's",
        "subcategories": [{
            "subcategory_parent": "Shoes",
            "subcategory": [
                {
                    "subcategory_id": "840",
                    "subcategory_label": "Casual"
                }
            ]}
        ]} 
 ],
  "prices": [
      {
        "price_label": "",
        "price_currency": "USD",
        "price_wholesale": 57.50,
        "price_retail": 125.00,
        "price_currency_retail": "USD"
      }
  ],
  "upcs": [
      {
        "sku_color_code": "1",
        "sku_size": "7",
        "upc": "888509010764",
        "inventory_available": "0"
      }, 
      {
        "sku_color_code": "1",
        "sku_size": "7.5",
        "upc": "888509010771",
        "inventory_available": "0"
      }, 
      {
        "sku_color_code": "1",
        "sku_size": "8",
        "upc": "888509010788",
        "inventory_available": "0"
      }, 
      }]

我在字符串变量中有Json对象。

有人可以告诉我如何在价格中访问“price_currency”吗?

 JSONObject json = new JSONObject(output);
           String sc = json.get("styles").toString();

我使用上面的方法获得了样式,

之后我使用下面的代码获得了样式。风格在风格内。

JSONObject json1 = new JSONObject(sc);
               String sc1 = json1.get("style").toString();

现在我想在价格中获得“price_currency”?我该怎么做?

1 个答案:

答案 0 :(得分:3)

您的价格在array范围内。您必须先通过索引

访问数组中的元素
JSONObject outputJson = new JSONObject(output);
JSONObject style = outputJson.getJsonObject("styles").getJsonArray("style").getJsonObject(0);
JSONObject price = style.getJsonArray("prices").getJsonObject(0);
String currency = price.getString("price_currency");

修改

JSONArray实施List<JSONObject>,您可以这样做:

for (JSONObject price : style.getJsonArray("prices")) {
     String currency = price.getString("price_currency");
}