迭代JSON数组对象

问题描述 投票:4回答:2

我正在查询并返回一个json字符串,为了这个例子,我将发布一个例子。我试图找出如何挖掘值数组并找到哪个标记为默认值。

示例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时,它返回以下内容。

{values=[{nameValuePairs={audioChannels=2.0, audioSampleRate=44100.0, bitrate=281656.0, checksums={nameValuePairs={md5=70DF3E21131F9F02C3F0A74F3664AB73}}......

如何正确浏览内容的值并找到isDefault的值?在示例JSON中没有isDefault = true的内容,所以它将默认为第一个对象。

似乎我只能将值作为字符串来定位,我是否必须将content作为JSONArray进行投射?

编辑:我似乎无法将mediaObject.content转换为JSONArray。 mediaObject.optJSONArray("content")返回null。我也尝试将它作为一个字符串,然后转换为JSONArray,没有占上风。

编辑2:发现数据的问题是什么,当我用gson解析json时,它弄乱了最终输出的数据。

所以我从new Gson().toJson(jsonObject);改为jsonObject.toString()),我现在可以使用optJSONArray来定位数组。为了将它们的数据恢复为JSONObject,我使用了JSONObject mediaObject = new JSONObject(mediaString);

GSON正在改变这些数据

java android json gson
2个回答
3
投票

您可以改为使用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 methodsJSONArray methods的链接


2
投票

你能做到吗?

JSONObject mJson = new JSONObject(inputString);

你为什么要用Gson?

© www.soinside.com 2019 - 2024. All rights reserved.