gpt4 book ai didi

java - 在对象内导航数组,Android Java

转载 作者:行者123 更新时间:2023-12-01 14:52:18 25 4
gpt4 key购买 nike

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

}

最佳答案

假设obj是通过解析发布的String获得的JSONObject

“results”是您使用

从对象获取的 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") 了......

关于java - 在对象内导航数组,Android Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14710293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com