简体   繁体   English

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

[英]How to get a given below value from json data?

I need to get the images array data how to put into the adapter and not required adapter code?我需要获取图像数组数据如何放入适配器而不需要适配器代码? Only loop and how to add that's important for me and then after second I can access option_value array.只有循环以及如何添加这对我很重要,然后在第二次之后我可以访问 option_value 数组。 First all images can be accessed then after a option_value array.首先可以访问所有图像,然后在 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"
        }
    }
}

You should really look into How to parse JsonObject / JsonArray in android.您应该真正研究如何在 android 中解析 JsonObject / JsonArray。

Please put your code of also what you have tried because people are here to help solve error/problem not to do coding.请把你的代码也写在你已经尝试过的地方,因为人们在这里帮助解决错误/问题而不是编码。

Here is code from which you can Parse your json这是您可以解析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
}

// code for option value // 选项值的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM