从JSONObject令牌流和树模型解析Jackson 2.1.1中不接收任何值

时间:2015-01-21 13:03:03

标签: android json jackson

美好的一天,我正在尝试使用jackson 2.1.1解析以下JSONObject(如下所示)我已经读过你可以将令牌流和节点树解析结合起来,到目前为止我已经提出了这个。

 private void parseForecast(JSONObject jobj2)  {

        String description = null;
        String condition = null;

        String list = jobj2.toString();

        try {
            JsonFactory f = new MappingJsonFactory();
            JsonParser parser = f.createJsonParser(list);
            parser.nextToken(); //{

            while(parser.nextToken() != JsonToken.END_OBJECT){
                String currentField =  parser.getCurrentName();
                Log.d(TAG, "currentField is " + currentField);

                if(currentField.equals("cod")){
                    parser.nextToken();

                }else if(currentField.equals("message")){
                    parser.nextToken();

                } else if(currentField.equals("city")){
                    parser.skipChildren();

                }else if(currentField.equals("cnt")) {
                    parser.nextToken();

                }else if(currentField.equals("list")){
                    while(parser.nextToken() != JsonToken.END_ARRAY){

                        Log.d(TAG, "reading the value as a tree"); //no logcat response after here
                        JsonNode node = parser.readValueAsTree();


                        for(JsonNode nodelist: node){

                            String date    = nodelist.get("dt").asText();

                   String day_temp = nodelist.get("temp").path("day").asText();
                  String main = nodelist.get("weather").get("main").textValue();

                            Log.d(TAG, "date from json nodelist is " + date);
                            Log.d(TAG, "day from json nodelist is " + day_temp);
                            Log.d(TAG, "main from json nodelist is " + main);

                        }


                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

可以看出,我正试图在"列表"中获取消息。令牌数组但是在logcat消息之后我没有得到logcat反馈" 以树的形式读取值"甚至是错误。即使我删除了for循环,仍然没有收到任何日志消息。有什么想法吗?我猜JsonNode以某种方式打破循环或其他东西。还是我错误地解析它?我不知所措。

Json对象

{
  "cod": "200",
  "message": 0.0029,
  "city": {
    "id": 2643743,
    "name": "London",
    "coord": {
      "lon": -0.12574,
      "lat": 51.50853
    },
    "country": "GB",
    "population": 0,
    "sys": {
      "population": 0
    }
  },
  "cnt": 3,
  "list": [
    {
      "dt": 1421755200,
      "temp": {
        "day": 271.55,
        "min": 271.21,
        "max": 275.15,
        "night": 275.15,
        "eve": 273.92,
        "morn": 271.21
      },
      "pressure": 1019.21,
      "humidity": 100,
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "speed": 2.26,
      "deg": 166,
      "clouds": 64,
      "rain": 1
    },
    {
      "dt": 1421841600,
      "temp": {
        "day": 277.17,
        "min": 275.34,
        "max": 277.3,
        "night": 275.34,
        "eve": 276.21,
        "morn": 275.66
      },
      "pressure": 1006.65,
      "humidity": 98,
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "speed": 8.18,
      "deg": 138,
      "clouds": 64,
      "rain": 2
    },
    {
      "dt": 1421928000,
      "temp": {
        "day": 276.04,
        "min": 273.06,
        "max": 276.3,
        "night": 273.94,
        "eve": 273.84,
        "morn": 274.23
      },
      "pressure": 1017.55,
      "humidity": 100,
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "sky is clear",
          "icon": "01d"
        }
      ],
      "speed": 3.14,
      "deg": 52,
      "clouds": 0
    }
  ]
}

0 个答案:

没有答案