簡體   English   中英

從Java中的Json Object獲取值

[英]Get a value from Json Object in java

"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”嗎? 我怎么做?

你的價格在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");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM