1

我有 JSON 对象

{
 "projector":"no",
 "video_conference":"no",
 "polycom":"no",
 "lcd":"no",
 "digital_phone":"no",
 "speaker_phone":"no"
}

如何将键存储在一个数组中,将值存储在单独的数组中?

4

3 回答 3

3

尝试GSON 将您的 java 对象转换为 json,反之亦然。

参考这个链接

http://code.google.com/p/google-gson/

于 2013-02-12T05:35:08.620 回答
1

我喜欢来自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()]);
于 2013-02-12T05:07:55.803 回答
1

你可以试试这个。

 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;

        }
于 2013-02-12T05:11:57.917 回答