安卓(JSONObject的)我怎样才能通过平JSON对象循环得到每个键和每个值 [英] Android (JSONObject) How can I loop through a flat JSON object to get each key and each value

查看:148
本文介绍了安卓(JSONObject的)我怎样才能通过平JSON对象循环得到每个键和每个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

在此先感谢。

推荐答案

使用的<一个href="http://developer.android.com/reference/org/json/JSONObject.html#keys%28%29"><$c$c>keys()迭代器遍历所有属性,并调用<一href="http://developer.android.com/reference/org/json/JSONObject.html#get%28java.lang.String%29"><$c$c>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!
    }
}

这篇关于安卓(JSONObject的)我怎样才能通过平JSON对象循环得到每个键和每个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆