Android (JSONObject)如何循环使用一个平面JSON对象来获取每个键和每个值

时间:2022-05-25 13:39:20

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

我的问题。很简单,但没有人问过,也没有回答。

I have a flat JSON:

我有一个平坦的JSON:

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

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.

我可以将它设置为JSONObject。但是,我不知道如何循环遍历每个项并获得它的键字符串和值字符串。

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.

有人能提供使用JSONObject的有效代码吗?我真正想要的是得到每一项的键和值w/o预先知道键或值。

Thanks in advance.

提前谢谢。

4 个解决方案

#1


262  

Use the keys() iterator to iterate over all the properties, and call get() for each.

使用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!
    }
}

#2


60  

Short version of Franci's answer:

弗朗西的简短版本:

for(Iterator<String> iter = json.keys();iter.hasNext();) {
    String key = iter.next();
    ...
}

#3


3  

You shold use the keys() or names() method. keys() will give you an iterator containing all the String property names in the object while names() will give you an array of all key String names.

您应该使用keys()或names()方法。keys()将提供一个迭代器,该迭代器包含对象中的所有字符串属性名,而name()将给您提供所有关键字符串名称的数组。

You can get the JSONObject documentation here

您可以在这里获得JSONObject文档

http://developer.android.com/reference/org/json/JSONObject.html

http://developer.android.com/reference/org/json/JSONObject.html

#4


-1  

Take a look at the JSONObject reference:

看看JSONObject的引用:

http://www.json.org/javadoc/org/json/JSONObject.html

http://www.json.org/javadoc/org/json/JSONObject.html

Without actually using the object, it looks like using either getNames() or keys() which returns an Iterator is the way to go.

如果不实际使用对象,它看起来就像使用getNames()或keys()返回迭代器的方法。

#1


262  

Use the keys() iterator to iterate over all the properties, and call get() for each.

使用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!
    }
}

#2


60  

Short version of Franci's answer:

弗朗西的简短版本:

for(Iterator<String> iter = json.keys();iter.hasNext();) {
    String key = iter.next();
    ...
}

#3


3  

You shold use the keys() or names() method. keys() will give you an iterator containing all the String property names in the object while names() will give you an array of all key String names.

您应该使用keys()或names()方法。keys()将提供一个迭代器,该迭代器包含对象中的所有字符串属性名,而name()将给您提供所有关键字符串名称的数组。

You can get the JSONObject documentation here

您可以在这里获得JSONObject文档

http://developer.android.com/reference/org/json/JSONObject.html

http://developer.android.com/reference/org/json/JSONObject.html

#4


-1  

Take a look at the JSONObject reference:

看看JSONObject的引用:

http://www.json.org/javadoc/org/json/JSONObject.html

http://www.json.org/javadoc/org/json/JSONObject.html

Without actually using the object, it looks like using either getNames() or keys() which returns an Iterator is the way to go.

如果不实际使用对象,它看起来就像使用getNames()或keys()返回迭代器的方法。