【问题标题】:Getting a JSON Left side Value获取 JSON 左侧值
【发布时间】:2018-12-11 08:41:13
【问题描述】:

我想得到数字“10449479”、“10449480”、“10449481”。

{
"xyz": {
"10449479": {
  "a": "22",
  "b": "33",
  "c": " - "
},
"10449480": {
  "a": "44",
  "b": "55",
  "c": " - "
  },
"10449481": {
  "a": "66",
  "b": "77",
  "c": " - "
 }}}

我需要获取 keys() 因为它们是未知的。我怎样才能得到这些数字。请帮忙。

public void onResponse(JSONObject response) {//From Volley

    try {
        JSONObject jarray = response.getJSONObject("xyz");

        Iterator<String> iter = jarray.keys();
        while (iter.hasNext()) {
            String key = iter.next();
            try {
                Object value = jarray.get(key);

            } catch (JSONException e) {
                // Something went wrong!
            }
        }


    } catch (JSONException e) {
        e.printStackTrace();


    }

}

我只能获取每个键内的嵌套值,如

{
  "a": "22",
  "b": "33",
  "c": " - "
}
//and other 2

如何获得左侧键。谢谢。

【问题讨论】:

  • 对那个对象也做同样的事情。再次获取键,然后获取值。

标签: android arrays json


【解决方案1】:

如果键是动态的,您可以使用 jsonobj.keys() 进行迭代

JSONObject JSONObj = new JSONObject(your_json_string);

// get all keys from json object
Iterator<String> iterator = JSONObj.keys();

while (iterator.hasNext()) {
    String key = iterator.next();
    Log.i("TAG","key:"+key +"--Value::"+JSONObj.optString(key);
}

【讨论】:

  • 确实如此,我使用哈希键的方式错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 2016-02-17
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
相关资源
最近更新 更多