JSON解析,结构辅助

时间:2013-10-09 23:59:34

标签: android json

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数组,直到' row'然后能够阅读每个' colx'消除'未处理的财产'

对于那可怕的英语质量感到抱歉,已经很晚了,我在描述它时感到很茫然。

1 个答案:

答案 0 :(得分: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.
}