【问题标题】:multilevel jsonObject parsing多级jsonObject解析
【发布时间】:2017-04-18 18:30:13
【问题描述】:

我只需要解码描述、温度、id 和名称!我该怎么做?

 {
    "cnt": 1,
    "list": [
        {
            "coord": {
            },
            "sys": {
            },
            "weather": [
                {
                    "id": 800,
                    "main": "Clear",
                    "description": "clear sky",
                    "icon": "01n"
                }
            ],
            "main": {
                "temp": 0.02,
                "pressure": 1025,
                "humidity": 68,
                "temp_min": -1,
                "temp_max": 1
            },
            "visibility": 10000,
            "wind": {
            },
            "clouds": {
            },
            "dt": 1492539576,
            "id": 524901,
            "name": "Moscow"
        }
    ]
}

【问题讨论】:

  • 你的 JSON 中有两个“id´s”,你想要哪个?
  • 我需要第二个“id”

标签: android json get android-volley


【解决方案1】:

使用此代码:

 try{
    JSONObject json = new JSONObject(jsonString);
    JSONObject list = json.getJSONArray("list");
    JSONObject object = list.getJSONObject(0);
    JSONArray weatherArray = object.getJSONArray("weather");
    for(int i=0;i<weatherArray.length();i++){
         JSONObject object_n = weatherArray.getJSONObject(i);
         String description = object_n.getString("description");
        }
    JSONObject main = object.getJSONObject("main");
    String temp = main.getString("temp");
    String name = object.getString("name");
    String id = object.getString("id");
     }
     catch (JSONException e){
           e.printStackTrace();
    }

【讨论】:

  • 不客气...确保将答案标记为正确..thankx
【解决方案2】:

试试这个:

    // Here is your sample JSON
    String sampleJson = "{\n" +
            "    \"cnt\": 1,\n" +
            "    \"list\": [\n" +
            "        {\n" +
            "            \"coord\": {\n" +
            "            },\n" +
            "            \"sys\": {\n" +
            "            },\n" +
            "            \"weather\": [\n" +
            "                {\n" +
            "                    \"id\": 800,\n" +
            "                    \"main\": \"Clear\",\n" +
            "                    \"description\": \"clear sky\",\n" +
            "                    \"icon\": \"01n\"\n" +
            "                }\n" +
            "            ],\n" +
            "            \"main\": {\n" +
            "                \"temp\": 0.02,\n" +
            "                \"pressure\": 1025,\n" +
            "                \"humidity\": 68,\n" +
            "                \"temp_min\": -1,\n" +
            "                \"temp_max\": 1\n" +
            "            },\n" +
            "            \"visibility\": 10000,\n" +
            "            \"wind\": {\n" +
            "            },\n" +
            "            \"clouds\": {\n" +
            "            },\n" +
            "            \"dt\": 1492539576,\n" +
            "            \"id\": 524901,\n" +
            "            \"name\": \"Moscow\"\n" +
            "        }\n" +
            "    ]\n" +
            "}";

使用parseMultilevelJSON(sampleJson)方法从上面的sampleJson数据中解析descriptiontempidname

public void parseMultilevelJSON(String jsonStr) {

    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            // List
            JSONArray listJsonArray = jsonObj.getJSONArray("list");
            // List-Item
            JSONObject itemJsonObject = listJsonArray.getJSONObject(0);

            // Weather
            JSONArray weatherJsonArray = itemJsonObject.getJSONArray("weather");
            // Weather-Item
            JSONObject weatherJsonObject = weatherJsonArray.getJSONObject(0);

            // Description
            String description = weatherJsonObject.getString("description");

            // Main
            JSONObject mainJsonObject = itemJsonObject.getJSONObject("main");
            // Temp
            double temp = mainJsonObject.getDouble("temp");

            // Id
            int id = itemJsonObject.getInt("id");
            // Name
            String name = itemJsonObject.getString("name");

            Log.d("SUCCESS", "JSON ITEM: " + "\nDescription: " + description
                    + "\nTemp: " + temp + "\nId: " + id + "\nName: " + name);

        } catch (final JSONException e) {
            Log.e("FAILED", "Json parsing error: " + e.getMessage());
        }
    }
}

输出:

3313-3313/com.ferdous.stackoverflowanswer D/SUCCESS: JSON ITEM: 
                                                     Description: clear sky
                                                     Temp: 0.02
                                                     Id: 524901
                                                     Name: Moscow

【讨论】:

    猜你喜欢
    • 2019-03-20
    • 1970-01-01
    • 2013-11-13
    • 2016-08-25
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多