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投稿された文字列を解析して取得した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")から1つだけ離れます...

于 2013-02-05T15:11:46.127 に答える