我有以下JSON:
{
"registration": {
"name": "Vik Kumar",
"first_name": "Vik",
"last_name": "Kumar",
"bloodGroup": "B-",
"gender": "male",
"birthday": "10\/31\/1983",
"email": "vik.ceo\u0040gmail.com",
"cellPhone": "1234123456",
"homePhone": "1234123457",
"officePhone": "1234123458",
"primaryAddress": "jdfjfgj",
"area": "jfdjdfj",
"location": {
"name": "Redwood Shores, California",
"id": 103107903062719
},
"subscribe": true,
"eyePledge": false,
"reference": "fgfgfgfg"
}
}
我使用以下代码来解析它:
JsonNode json = new ObjectMapper().readTree(jsonString);
JsonNode registration_fields = json.get("registration");
Iterator<String> fieldNames = registration_fields.getFieldNames();
while(fieldNames.hasNext()){
String fieldName = fieldNames.next();
String fieldValue = registration_fields.get(fieldName).asText();
System.out.println(fieldName+" : "+fieldValue);
}
这样可以正常工作并打印除了位置之外的所有值,这是另一种嵌套级别。我尝试了与上面的代码相同的技巧来传递json.get(“location”),但这不起作用。请建议如何使其适用于位置。
答案 0 :(得分:1)
由于location
嵌套在registration
中,您需要使用:
registration_fields.get("location");
得到它。但是它不是已经由while循环处理了,为什么你需要单独获取它?