迭代 JSON 数组对象
Iterate JSON array object
我正在查询并返回一个 json 字符串,为了这个例子,我将 post 作为一个例子。我想弄清楚我将如何挖掘值数组并找到标记为默认值的值。
示例JSON
{
"id": "333706819617",
"guid": "4aCdriCG0WvfYEUkFf8_xqQEFxgwgNU8",
"title": "Test entry",
"author": "",
"description": "Desc",
"added": 1411702963000,
"content": [
{
"audioChannels": 2,
"audioSampleRate": 44100,
"bitrate": 281656,
"checksums": {
"md5": "70DF3E21131F9F02C3F0A74F3664AB73"
},
"contentType": "audio",
"duration": 43.258,
"expression": "full",
"fileSize": 1522986,
"frameRate": 0.0,
"format": "AAC",
"height": 288,
"isDefault": false,
"language": "en",
"sourceTime": 0.0,
"url": "http://example.com/dZiASoxchRyS",
"width": 352
},
{
"audioChannels": 2,
"audioSampleRate": 44100,
"bitrate": 160000,
"checksums": {
"md5": "3AC622D31B9DED37792CC7FF2F086BE6"
},
"contentType": "audio",
"duration": 43.206,
"expression": "full",
"fileSize": 866504,
"frameRate": 0.0,
"format": "MP3",
"height": 0,
"isDefault": false,
"language": "",
"sourceTime": 0.0,
"url": "http://example.com/59M_PSFgGGXE",
"width": 0
}
],
"thumbnails": [
{
"audioChannels": 0,
"audioSampleRate": 0,
"bitrate": 0,
"checksums": {
"md5": "BE8C98A07B3FE9020BFA464C42112999"
},
"contentType": "image",
"duration": 0.0,
"expression": "full",
"fileSize": 20379,
"frameRate": 0.0,
"format": "JPEG",
"height": 256,
"isDefault": true,
"language": "",
"sourceTime": 0.0,
"url": "http://img.example.com/waveform.jpg",
"width": 256
}
]
}
我将 JSON 字符串转换回 JSONObject
JSONObject mediaObject = new Gson().fromJson(mediaString, JSONObject.class);
String content = mediaObject.optString("content");
当我输出content
时,它returns如下。
{values=[{nameValuePairs={audioChannels=2.0, audioSampleRate=44100.0, bitrate=281656.0, checksums={nameValuePairs={md5=70DF3E21131F9F02C3F0A74F3664AB73}}......
如何正确遍历内容的值并找到 isDefault
的值?在示例 JSON 中,isDefault = true
中没有内容,因此它将默认为第一个对象。
似乎我只能将值定位为字符串,是否必须将 content
转换为 JSONArray
?
编辑: 我似乎无法将 mediaObject.content
转换为 JSON 数组。 mediaObject.optJSONArray("content")
returns 空。我也试过把它作为一个字符串然后转换成一个 JSONArray 没有 prevail.
编辑 2: 发现数据有什么问题,当我用 gson 解析 json 时,它弄乱了最终输出的数据。
所以我从 new Gson().toJson(jsonObject);
更改为 jsonObject.toString())
,现在我可以使用 optJSONArray
定位数组。为了将它们的数据返回到 JSON 对象中,我使用了 JSONObject mediaObject = new JSONObject(mediaString);
GSON 正在更改数据
您可以获取 JSONArray,而不是从 JSONObject 中提取内容的字符串值
JSONArray content = mediaObject.getJSONArray("content");
现在您可以使用非常传统的 for 循环遍历数组中的对象
for(int i = 0; i < content.length(); i++) {
JSONObject mediaItem = content.getJSONObject(i);
boolean itemIsDefault = mediaItem.getBoolean("isDefault");
}
这里是所有JSONObject methods and JSONArray methods
的链接
你能不能不只是
JSONObject mJson = new JSONObject(inputString);
你为什么使用 Gson?
我正在查询并返回一个 json 字符串,为了这个例子,我将 post 作为一个例子。我想弄清楚我将如何挖掘值数组并找到标记为默认值的值。
示例JSON
{
"id": "333706819617",
"guid": "4aCdriCG0WvfYEUkFf8_xqQEFxgwgNU8",
"title": "Test entry",
"author": "",
"description": "Desc",
"added": 1411702963000,
"content": [
{
"audioChannels": 2,
"audioSampleRate": 44100,
"bitrate": 281656,
"checksums": {
"md5": "70DF3E21131F9F02C3F0A74F3664AB73"
},
"contentType": "audio",
"duration": 43.258,
"expression": "full",
"fileSize": 1522986,
"frameRate": 0.0,
"format": "AAC",
"height": 288,
"isDefault": false,
"language": "en",
"sourceTime": 0.0,
"url": "http://example.com/dZiASoxchRyS",
"width": 352
},
{
"audioChannels": 2,
"audioSampleRate": 44100,
"bitrate": 160000,
"checksums": {
"md5": "3AC622D31B9DED37792CC7FF2F086BE6"
},
"contentType": "audio",
"duration": 43.206,
"expression": "full",
"fileSize": 866504,
"frameRate": 0.0,
"format": "MP3",
"height": 0,
"isDefault": false,
"language": "",
"sourceTime": 0.0,
"url": "http://example.com/59M_PSFgGGXE",
"width": 0
}
],
"thumbnails": [
{
"audioChannels": 0,
"audioSampleRate": 0,
"bitrate": 0,
"checksums": {
"md5": "BE8C98A07B3FE9020BFA464C42112999"
},
"contentType": "image",
"duration": 0.0,
"expression": "full",
"fileSize": 20379,
"frameRate": 0.0,
"format": "JPEG",
"height": 256,
"isDefault": true,
"language": "",
"sourceTime": 0.0,
"url": "http://img.example.com/waveform.jpg",
"width": 256
}
]
}
我将 JSON 字符串转换回 JSONObject
JSONObject mediaObject = new Gson().fromJson(mediaString, JSONObject.class);
String content = mediaObject.optString("content");
当我输出content
时,它returns如下。
{values=[{nameValuePairs={audioChannels=2.0, audioSampleRate=44100.0, bitrate=281656.0, checksums={nameValuePairs={md5=70DF3E21131F9F02C3F0A74F3664AB73}}......
如何正确遍历内容的值并找到 isDefault
的值?在示例 JSON 中,isDefault = true
中没有内容,因此它将默认为第一个对象。
似乎我只能将值定位为字符串,是否必须将 content
转换为 JSONArray
?
编辑: 我似乎无法将 mediaObject.content
转换为 JSON 数组。 mediaObject.optJSONArray("content")
returns 空。我也试过把它作为一个字符串然后转换成一个 JSONArray 没有 prevail.
编辑 2: 发现数据有什么问题,当我用 gson 解析 json 时,它弄乱了最终输出的数据。
所以我从 new Gson().toJson(jsonObject);
更改为 jsonObject.toString())
,现在我可以使用 optJSONArray
定位数组。为了将它们的数据返回到 JSON 对象中,我使用了 JSONObject mediaObject = new JSONObject(mediaString);
GSON 正在更改数据
您可以获取 JSONArray,而不是从 JSONObject 中提取内容的字符串值
JSONArray content = mediaObject.getJSONArray("content");
现在您可以使用非常传统的 for 循环遍历数组中的对象
for(int i = 0; i < content.length(); i++) {
JSONObject mediaItem = content.getJSONObject(i);
boolean itemIsDefault = mediaItem.getBoolean("isDefault");
}
这里是所有JSONObject methods and JSONArray methods
的链接你能不能不只是
JSONObject mJson = new JSONObject(inputString);
你为什么使用 Gson?