安卓(JSONObject的)我怎样才能通过平JSON对象循环得到每个键和每个值对象、JSONObject、JSON

2023-09-12 07:15:02 作者:痴心妄想

我的疑问句。很简单,但没有人问也不回答了。

My ques. is simple yet no one has asked it nor answered it.

我有一个平坦的JSON:

I have a flat JSON:

{"number1":"value1", "number2":"value2", "number3":"value3" }

我可以将其设置为JSONObject的就好了。但是,我不通过各项目知道如何循环并获得其关键字符串,字符串值。

I can set it to a JSONObject just fine. However, I don't know how to loop through each item and get its key string and value string.

任何人都可以提供一个高效的code使用的JSONObject?我真正想要的是让每个项目的键和值W / O pre-知道密钥,也没有价值。

Can anyone provide an efficient code for using JSONObject? What I really want is to get each item's key and value w/o pre-knowing the key nor value.

在此先感谢。

推荐答案

使用的keys()迭代器遍历所有属性,并调用get()每个

Iterator<String> iter = json.keys();
while (iter.hasNext()) {
    String key = iter.next();
    try {
        Object value = json.get(key);
    } catch (JSONException e) {
        // Something went wrong!
    }
}