0

我在下面有以下 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"
}

}

4

1 回答 1

1

假设obj是解析posted String得到的一个JSONObject

“结果”是您从对象中获取的 JSONArray 的关键

JSONArray results = obj.getJSONObject("d").getJSONArray("results")

JSONArray 为您可能遇到的每种数据类型提供了 get*(int index)。

在您的情况下,“结果”是 JSONObject 的数组。您可以使用以下方法循环遍历它:

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

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

于 2013-02-05T15:11:46.127 回答