[英]Parse JSON with unknown key?
您可以使用Iterator。
使用Iterator,可以在任一方向上迭代列表的所有元素。 您可以通過調用next()方法訪問next元素。
JSONObject reader = new JSONObject(success);
Iterator iteratorObj = reader .keys();
ArrayList<String> al_getAllKeys=new ArrayList<String>();
while (iteratorObj.hasNext())
{
String getJsonObj = (String)iteratorObj.next();
System.out.println("KEY: " + "------>" + getJsonObj);
}
您可以使用jackson工具將json轉換為pojo以下是代碼片段
public void parse(String json) {
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
JsonNode rootNode = mapper.readTree(json);
Iterator<Map.Entry<String,JsonNode>> fieldsIterator = rootNode.fields();
while (fieldsIterator.hasNext()) {
Map.Entry<String,JsonNode> field = fieldsIterator.next();
System.out.println("Key: " + field.getKey() + "\tValue:" + field.getValue());
}
}
HapPy C @ ding .. !!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.