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.
}
相同操作的语法是什么,但使用'Jackson'解析库?
我目前有:
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://pastebin.com/rhD2d9fx