尝试阅读下面的Json消息
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{
"firstName": "Anna",
"lastName": "Smith"
},
{
"firstName": "Peter",
"lastName": "Jones"
}
]
}
我想在java中读取所有关键字,例如employees,firstName,lastName等。 在XML中,我们在解析时在dom对象中指定*并获取所有节点名称,如何在java中执行此操作?
答案 0 :(得分:0)
使用org.json库。
示例:
import org.json.*;
String myJSONString = // put here your json object
JSONObject object = new JSONObject(myJSONString);
// get all the keys
String[] keys = JSONObject.getNames(object);
// iterate over them
for (String key : keys)
{
// retrieve the values
Object value = object.get(key);
// if you just have strings:
String value = (String) object.get(key);
}