0

我想将此 jason 解析为 android 应用程序的 java 代码:

{
"name":"London",
"coord":{"lon":-0.12574,"lat":51.50853},
"country":"GB",
"cnt":6,
"list":[{
         "dt":1377345600,
         "temp":{                   
                   "min":290.42,
                   "max":294.3,
                   "weather":[{
                                 "id":501,
                                 "main":"Rain",
                                 "icon":"10d"}],
        {
         "dt":1377432000,
         "temp":{
                   "min":289.81,
                   "max":296.92,
                   "weather":[{
                                 "id":800,
                                 "main":"Clear",
                                 "icon":"01d"}],}

我怎样才能做到这一点?我知道如何解析单个对象数组,但子对象如何工作?

这是完整的 json 文件。在 jsonlint.com 中验证

{
"cod": "200",
"message": 0.012,
"city": {
    "id": 2643743,
    "name": "London",
    "coord": {
        "lon": -0.12574,
        "lat": 51.50853
    },
    "country": "GB",
    "population": 1000000
},
"cnt": 6,
"list": [
    {
        "dt": 1377345600,
        "temp": {
            "day": 293.39,
            "min": 290.42,
            "max": 294.3,
            "night": 290.67,
            "eve": 292.47,
            "morn": 290.42
        },
        "pressure": 1013.85,
        "humidity": 83,
        "weather": [
            {
                "id": 501,
                "main": "Rain",
                "description": "moderate rain",
                "icon": "10d"
            }
        ],
        "speed": 1.36,
        "deg": 276,
        "clouds": 100,
        "rain": 4
    },
    {
        "dt": 1377432000,
        "temp": {
            "day": 295.53,
            "min": 289.81,
            "max": 296.92,
            "night": 289.81,
            "eve": 295.44,
            "morn": 289.83
        },
        "pressure": 1019.06,
        "humidity": 73,
        "weather": [
            {
                "id": 800,
                "main": "Clear",
                "description": "sky is clear",
                "icon": "01d"
            }
        ],
        "speed": 3.36,
        "deg": 342,
        "clouds": 0
    },
    {
        "dt": 1377518400,
        "temp": {
            "day": 296.03,
            "min": 286.39,
            "max": 296.64,
            "night": 286.85,
            "eve": 295.04,
            "morn": 286.39
        },
        "pressure": 1024.06,
        "humidity": 65,
        "weather": [
            {
                "id": 800,
                "main": "Clear",
                "description": "sky is clear",
                "icon": "01d"
            }
        ],
        "speed": 2.06,
        "deg": 32,
        "clouds": 0
    },
    {
        "dt": 1377604800,
        "temp": {
            "day": 296.46,
            "min": 284.3,
            "max": 296.82,
            "night": 289.19,
            "eve": 293.97,
            "morn": 284.3
        },
        "pressure": 1026.11,
        "humidity": 57,
        "weather": [
            {
                "id": 500,
                "main": "Rain",
                "description": "light rain",
                "icon": "10d"
            }
        ],
        "speed": 1.5,
        "deg": 226,
        "clouds": 12,
        "rain": 2
    },
    {
        "dt": 1377691200,
        "temp": {
            "day": 293.48,
            "min": 287.45,
            "max": 294.79,
            "night": 291.74,
            "eve": 293.88,
            "morn": 287.45
        },
        "pressure": 1031.75,
        "humidity": 70,
        "weather": [
            {
                "id": 802,
                "main": "Clouds",
                "description": "scattered clouds",
                "icon": "03d"
            }
        ],
        "speed": 2,
        "deg": 40,
        "clouds": 32
    },
    {
        "dt": 1377777600,
        "temp": {
            "day": 295.06,
            "min": 282.71,
            "max": 295.06,
            "night": 291.28,
            "eve": 295.06,
            "morn": 282.71
        },
        "pressure": 1030.87,
        "humidity": 0,
        "weather": [
            {
                "id": 500,
                "main": "Rain",
                "description": "light rain",
                "icon": "10d"
            }
        ],
        "speed": 6.03,
        "deg": 315,
        "clouds": 62
    }
]

}

4

3 回答 3

3

使用此方法动态迭代 json

private void parseJson(JSONObject data) {

        if (data != null) {
            Iterator<String> it = data.keys();
            while (it.hasNext()) {
                String key = it.next();

                try {
                    if (data.get(key) instanceof JSONArray) {
                        JSONArray arry = data.getJSONArray(key);
                        int size = arry.length();
                        for (int i = 0; i < size; i++) {
                            parseJson(arry.getJSONObject(i));
                        }
                    } else if (data.get(key) instanceof JSONObject) {
                        parseJson(data.getJSONObject(key));
                    } else {
                        System.out.println("" + key + " : " + data.optString(key));
                    }
                } catch (Throwable e) {
                    System.out.println("" + key + " : " + data.optString(key));
                    e.printStackTrace();

                }
            }
        }
    }
于 2013-08-24T11:06:25.220 回答
1

应该是这样的

 {
   "name":"London",
   "coord":{"lon":-0.12574,"lat":51.50853},
   "country":"GB",
   "cnt":6,
   "list":[{
     "dt":1377345600,
     "temp":{                   
               "min":290.42,
               "max":294.3,
               "weather":[{
                             "id":501,
                             "main":"Rain",
                             "icon":"10d"}]
            }
    },
    {
     "dt":1377432000,
     "temp":{
               "min":289.81,
               "max":296.92,
               "weather":[{
                             "id":800,
                             "main":"Clear",
                             "icon":"01d"}]
    }
 }]
}


您忘记添加括号和逗号。你可以参考这个 http://json.parser.online.fr/

于 2013-08-24T11:04:14.340 回答
0

您可以使用 json-simple。参考http://code.google.com/p/json-simple/

于 2013-08-24T12:48:40.223 回答