我有 JSON 对象
{
"projector":"no",
"video_conference":"no",
"polycom":"no",
"lcd":"no",
"digital_phone":"no",
"speaker_phone":"no"
}
如何将键存储在一个数组中,将值存储在单独的数组中?
我喜欢来自http://codehaus.org/的 Jackson用于 JSON 解析。
String text = "{ \"projector\":\"no\", \"video_conference\":\"no\", \"polycom\":\"no\", \"lcd\":\"no\", \"digital_phone\":\"no\", \"speaker_phone\":\"no\" }";
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(text, Map.class);
Set<String> k = map.keySet();
Collection<Object> v = map.values();
String[] keys = k.toArray(new String[k.size()]);
String[] values = v.toArray(new String[v.size()]);
你可以试试这个。
String s = " { "projector":"no", "video_conference":"no", "polycom":"no", "lcd":"no", "digital_phone":"no", "speaker_phone":"no" }";
JSONObject jObject = new JSONObject(s);
JSONObject menu = jObject.getJSONObject("projector");
Iterator iter = menu.keys();
String[] keyArr = new String();
String[] valArr = new String();
int count = 0;
while(iter.hasNext()){
keyArr[count] = (String)iter.next();
valArr[count] = menu.getString(key);
count +=1;
}