0
JsonFactory f = new MappingJsonFactory();

                        JsonParser jp = f.createParser(res);

                        JsonToken current;
                        current = jp.nextToken();
                        if (current != JsonToken.START_OBJECT) {
                            System.out.println("Error: root should be object: quiting.");
                            return;
                        }
                        while (jp.nextToken() != JsonToken.END_OBJECT) {
                            String fieldName = jp.getCurrentName();
                            // move from field name to field value
                            current = jp.nextToken();
                            if (fieldName.equals("row")) {
                                if (current == JsonToken.START_ARRAY) {

                                    while (jp.nextToken() != JsonToken.END_ARRAY) {

                                        JsonNode node = jp.readValueAsTree();
                                        Log.e("KFF", node.get("col0").asText());

                                    }
                                } else {
                                    Log.e("KFF", "Error: records should be an array: skipping.");
                                    jp.skipChildren();
                                }
                            } else {
                                Log.e("KFF", "Unprocessed property: " + fieldName);
                                jp.skipChildren();
                            }
                        }


                    } catch (JsonParseException e) {
                        e.printStackTrace();
                    } catch (JsonProcessingException e) {

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

                        e.printStackTrace();
                    }
                }
  • http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%22http%3A%2F%2Fichart.finance.yahoo.com%2Ftable.csv%3Fs%3DYHOO%26a%3D11%26b%3D10%26c%3D2011%26d%3D10%26e%3D10%26f%3D2013%26g%3Dd%22%3B&format=json&diagnostics=true&callback=

我目前正在使用上面的方法来解析来自网络的 JSON 响应(使用 JACKSON JSON PARSING LIBARY),但是我不知道如何解析嵌套的 json 数组,例如以下,即如何深入每个json 数组直到“行”,然后能够读取每个“colx”消除“未处理的属性”

对不起,英语的可怕质量,已经很晚了,我在描述它方面不知所措。

4

1 回答 1

1

像这样的东西应该适合你。免责声明 - 完全未经测试。不过,这将使您朝着正确的方向前进。

// Assuming the json is in a String called jsonString

JSONObject jsonObj = new JSONObject(jsonString);
JSONArray jsonArray = jsonObj.getJSONObject("query").getJSONObject("results").getJSONArray("row");

int arrayCount = jsonArray.length();
for(int i=0; i < arrayCount; i++){
      JSONObject jsonData = jsonArray.getJSONObject(i);
      String col0 = jsonData.getString("col0");
      String col1 = jsonData.getString("col1");
      // etc, etc ...
      // put those values in arrays or whatever here.
}
于 2013-10-10T00:27:30.000 回答