如何从json数据中获得给定的以下值?

时间:2015-07-04 04:35:22

标签: android arrays json jsonobject

我需要获取图像数组数据如何放入适配器而不是必需的适配器代码?只有循环以及如何添加对我来说很重要 然后我可以访问option_value数组。 首先,可以在option_value数组之后访问所有图像。

  {
    "success": true,
    "data": {
        "id": "50",
        "seo_h1": "",
        "name": "Shirt 10001",
        "manufacturer": "",
        "sku": "",
        "model": "10001",
        "image": "http://api.yellowskydemo.com/image/cache/data/shirts/7228-500x500.jpg",
        "images": [
            "http://api.yellowskydemo.com/image/cache/data/shirts/13-500x500.jpg",
            "http://api.yellowskydemo.com/image/cache/data/shirts/302-500x500.jpg",
            "http://api.yellowskydemo.com/image/cache/data/shirts/5-500x500.jpg",
            "http://api.yellowskydemo.com/image/cache/data/shirts/205-500x500.jpg"
        ],
        "price": "$540.00",
        "rating": 0,
        "description": "<p>Fasten your fashion belts, for this is the ultimate edit of swanky casual wear by Ralph Lauren! For men who are born with a strong sense of style, the American powerhouse packs a colourful punch with this high-fash collection of shirts and sporty, monogrammed polos.</p>\r\n\r\n<p>Fasten your fashion belts, for this is the ultimate edit of swanky casual wear by Ralph Lauren! For men who are born with a strong sense of style, the American powerhouse packs a colourful punch with this high-fash collection of shirts and sporty, monogrammed polos.</p>\r\n",
        "attribute_groups": [],
        "special": "",
        "discounts": [],
        "options": [
            {
                "name": "Size",
                "type": "select",
                "option_value": [
                    {
                        "image": "http://api.yellowskydemo.com/image/cache/no_image-100x100.jpg",
                        "price": "$50.00",
                        "price_prefix": "-",
                        "product_option_value_id": "17",
                        "option_value_id": "55",
                        "name": "L-40",
                        "quantity": "99"
                    },
                    {
                        "image": "http://api.yellowskydemo.com/image/cache/no_image-100x100.jpg",
                        "price": "$40.00",
                        "price_prefix": "+",
                        "product_option_value_id": "18",
                        "option_value_id": "57",
                        "name": "XXL-44",
                        "quantity": "100"
                    }
                ],
                "required": "1",
                "product_option_id": "227",
                "option_id": "14"
            }
        ],
        "minimum": "1",
        "meta_description": "",
        "meta_keyword": "",
        "tag": "",
        "upc": "",
        "ean": "",
        "jan": "",
        "isbn": "",
        "mpn": "",
        "location": "",
        "stock_status": "Out Of Stock",
        "manufacturer_id": null,
        "tax_class_id": "0",
        "date_available": "2014-08-12",
        "weight": "0.00000000",
        "weight_class_id": "1",
        "length": "0.00000000",
        "width": "0.00000000",
        "height": "0.00000000",
        "length_class_id": "1",
        "subtract": "0",
        "sort_order": "1",
        "status": "1",
        "date_added": "2014-08-13 12:05:56",
        "date_modified": "2015-06-30 11:19:39",
        "viewed": "8",
        "weight_class": "kg",
        "length_class": "cm",
        "reward": "0",
        "points": "0",
        "category": [
            {
                "name": "Shirts",
                "id": "59"
            },
            {
                "name": "Casual Shirts",
                "id": "60"
            }
        ],
        "quantity": "99",
        "reviews": {
            "review_total": "0"
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你应该深入研究如何在android中解析JsonObject / JsonArray。

请将您的代码也放在您尝试的内容中,因为人们在这里帮助解决错误/问题而不是编码。

以下是您可以解析json的代码

JSONObject jsonObject = new JSONObject();
JSONObject dataObject = jsonObject.getJSONObject("data");
JSONArray imagesArray = dataObject.getJSONArray("images");
ArrayList<String> listOfImagesUrl = new ArrayList<>();
for(int i = 0; i < imagesArray.length(); i++)
{
     listOfImagesUrl.add(imagesArray.getString(i)); // listOfImages
}

//选项值的代码

JSONArray optionsArray = dataObject.getJSONArray("options");
for(int i = 0; i < optionsArray.length(); i++)
{
   JSONObject option = optionsArray.getJSONObject(i);
   JSONArray optionsValueArray = option.getJSONArray("option_value");
   for(int j = 0 ; j < optionsValueArray.length(); j++)
   {
      JSONObject optionValueObject = optionsValueArray.getJSONObject(j);
      String image = optionValueObject.getString("image");
      String price = optionValueObject.getString("price");
      String price_prefix = optionValueObject.getString("price_prefix");
      //same like this
   }
}