在对象,Android Java中导航数组

时间:2013-02-05 14:50:53

标签: java android json

我在下面有以下JSON对象,我想知道如何获取MediaUrl的结果。我发现了很多关于如何解析数组或对象的教程,但我找不到任何关于如何在对象内部循环数组的内容。

{
"d": {
    "results": [
        {
            "__metadata": {
                "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Acanthurus achilles'&Adult='Strict'&ImageFilters='Size:Small+Aspect:Square'&Market='en-US'&$skip=0&$top=1",
                "type": "ImageResult"
            },
            "ID": "17866c98-31f2-4488-9512-e47785301117",
            "Title": "acanthurus achilles acanthuridae poissons chirurgiens",
            "MediaUrl": "http://www.aquariophilie62.fr/images/poissons-recifal/thumbs/Acanthurus-achilles-.jpg",
            "SourceUrl": "http://www.aquariophilie62.fr/fiches-poissons-recifaux/",
            "DisplayUrl": "www.aquariophilie62.fr/fiches-poissons-recifaux",
            "Width": "100",
            "Height": "100",
            "FileSize": "19633",
            "ContentType": "image/jpeg",
            "Thumbnail": {
                "__metadata": {
                    "type": "Bing.Thumbnail"
                },
                "MediaUrl": "http://ts2.mm.bing.net/th?id=H.4820238373750001&pid=15.1",
                "ContentType": "image/jpg",
                "Width": "100",
                "Height": "100",
                "FileSize": "2509"
            }
        },
        {
            "__metadata": {
                "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Acanthurus achilles'&Adult='Strict'&ImageFilters='Size:Small+Aspect:Square'&Market='en-US'&$skip=1&$top=1",
                "type": "ImageResult"
            },
            "ID": "e2a96fba-f8b2-4212-8477-1bf36131d61c",
            "Title": "100x100px-LS-8c425bd7_47876-000676_Acanthurus_achilles.jpg",
            "MediaUrl": "http://cdn.saltwaterfish.com/8/8c/100x100px-LS-8c425bd7_47876-000676_Acanthurus_achilles.jpg",
            "SourceUrl": "http://forums.saltwaterfish.com/t/346818/humu-humu-trigger-shrimps",
            "DisplayUrl": "forums.saltwaterfish.com/t/346818/humu-humu-trigger-shrimps",
            "Width": "100",
            "Height": "100",
            "FileSize": "8185",
            "ContentType": "image/jpeg",
            "Thumbnail": {
                "__metadata": {
                    "type": "Bing.Thumbnail"
                },
                "MediaUrl": "http://ts2.mm.bing.net/th?id=H.4781768365638869&pid=15.1",
                "ContentType": "image/jpg",
                "Width": "100",
                "Height": "100",
                "FileSize": "2234"
            }
        },
        {
            "__metadata": {
                "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Acanthurus achilles'&Adult='Strict'&ImageFilters='Size:Small+Aspect:Square'&Market='en-US'&$skip=2&$top=1",
                "type": "ImageResult"
            },
            "ID": "11d79168-8fd1-49d9-9d06-c54208cecf28",
            "Title": "acanthurus bahianus acanthuridae poissons chirurgiens",
            "MediaUrl": "http://www.aquariophilie62.fr/images/poissons-recifal/thumbs/Acanthurus-bahianus-.jpg",
            "SourceUrl": "http://www.aquariophilie62.fr/fiches-poissons-recifaux/",
            "DisplayUrl": "www.aquariophilie62.fr/fiches-poissons-recifaux",
            "Width": "100",
            "Height": "100",
            "FileSize": "19105",
            "ContentType": "image/jpeg",
            "Thumbnail": {
                "__metadata": {
                    "type": "Bing.Thumbnail"
                },
                "MediaUrl": "http://ts2.mm.bing.net/th?id=H.4820238373749985&pid=15.1",
                "ContentType": "image/jpg",
                "Width": "100",
                "Height": "100",
                "FileSize": "2373"
            }
        }
    ],
    "__next": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Acanthurus%20achilles'&Adult='Strict'&ImageFilters='Size:Small+Aspect:Square'&Market='en-US'&$skip=3&$top=3"
}

}

1 个答案:

答案 0 :(得分:1)

假设obj是通过解析发布的字符串

获得的JSONObject

“results”是您使用

从对象获取的JSONArray的关键
JSONArray results = obj.getJSONObject("d").getJSONArray("results")

JSONArray为您可能遇到的每种类型的数据获取*(int index)。

在您的情况下,“results”是一个JSONObject数组。你可以使用:

循环它
for (int i = 0; i < results.length(); i++) {
    JSONObject oneResult = results.getJSONObject(i);
}

一旦你有了JSONObject,MediaUrl就只有一个getString(“MediaUrl”)......