获取JSONArray中的值

时间:2014-05-17 23:15:41

标签: android json

嘿,我有这个json数组

[
    {
        "id": 1,
        "productName": "Audi R8",
        "description": "The best of Audi",
        "cost": 1000000,
        "rrp": 1500000,
        "product_category": [
            {
                "id": 1,
                "category": {
                    "id": 1,
                    "categoryname": "Supercars"
                }
            },
            {
                "id": 2,
                "category": {
                    "id": 2,
                    "categoryname": "Sportscars"
                }
            }
        ]
    },
    {
        "id": 2,
        "productName": "Nissan G-TR",
        "description": "The best of Nissan",
        "cost": 950000,
        "rrp": 1000000,
        "product_category": [
            {
                "id": 3,
                "category": {
                    "id": 2,
                    "categoryname": "Sportscars"
                }
            }
        ]
    },
    {
        "id": 3,
        "productName": "Volvo S60",
        "description": "fast car",
        "cost": 1000000,
        "rrp": 2000000,
        "product_category": [
            {
                "id": 4,
                "category": {
                    "id": 2,
                    "categoryname": "Sportscars"
                }
            }
        ]
    },
    {
        "id": 4,
        "productName": "Volvo V70",
        "description": "A good family car",
        "cost": 1500000,
        "rrp": 2000000,
        "product_category": [
            {
                "id": 5,
                "category": {
                    "id": 3,
                    "categoryname": "Volvo"
                }
            }
        ]
    },
    {
        "id": 5,
        "productName": "Volvo V50",
        "description": "Swedish car",
        "cost": 1000000,
        "rrp": 2000000,
        "product_category": [
            {
                "id": 6,
                "category": {
                    "id": 3,
                    "categoryname": "Volvo"
                }
            }
        ]
    },
    {
        "id": 6,
        "productName": "Ford Focus",
        "description": "A Ford car",
        "cost": 100000,
        "rrp": 150000,
        "product_category": [
            {
                "id": 7,
                "category": {
                    "id": 5,
                    "categoryname": "Ford"
                }
            }
        ]
    },
    {
        "id": 7,
        "productName": "Ford Mondeo",
        "description": "A big Ford",
        "cost": 180000,
        "rrp": 200000,
        "product_category": [
            {
                "id": 8,
                "category": {
                    "id": 5,
                    "categoryname": "Ford"
                }
            }
        ]
    },
    {
        "id": 8,
        "productName": "LaFerrari",
        "description": "a fast car",
        "cost": 2000000,
        "rrp": 3000000,
        "product_category": [
            {
                "id": 9,
                "category": {
                    "id": 1,
                    "categoryname": "Supercars"
                }
            },
            {
                "id": 10,
                "category": {
                    "id": 2,
                    "categoryname": "Sportscars"
                }
            }
        ]
    },
    {
        "id": 9,
        "productName": "Saab 9-3",
        "description": "a car From Saab",
        "cost": 10000,
        "rrp": 333300,
        "product_category": [
            {
                "id": 11,
                "category": {
                    "id": 4,
                    "categoryname": "Saab"
                }
            }
        ]
    }
]

我如何获得值categoryname所以我可以将它保存在android的这个textview中? TextView category =(TextView)productListItem                     .findViewById(R.id.product_cost);

1 个答案:

答案 0 :(得分:0)

整个JSON对象是JSONArray吗?所以它会像

getJSONArray("NameOfYourMainArray").getJSONObject(indexOfTheObjectYouWant).getJSONArray("product_category").getJSONObject(indexOfTheObjectYouWant).getString("categoryname");

请注意,NameOfYourMainArray是一个字符串,而indexOfTheObjectYouWant是一个整数。

干杯