如何解析JSON与Android上的任意键? [英] How to parse JSON with any key on android?

查看:140
本文介绍了如何解析JSON与Android上的任意键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解析JSON与Android上的任意键。 JSON数据是由任意键,数组和值。 这里有JSON数据和我的工作code。 我想用JSON解析把JSON数据类数据。

I want to parse JSON with any key on android. JSON data are consist of any key, array and values. here are JSON data and my working code. I want to put json data to class data by using JSON parsing.

JSON数据:

{
   "d520c8c17c8e":
        {   "id":"25689123",
            "number":"0e31b1994"
        },
   "d183c6e9913d":
        {
           "id":"25689123",
           "number":"0e31b1994"
         },
   "content":"8090986565488990",
   "text":"hello",
   "status":"ok"
}

我的code:

my code :

public static MyInfo getMyInfo(JSONObject json) {
    MyInfo info = new MyInfo();
    if (json == null)
        return null;
    try {

        info.setContent(json.getString("content"));
        info.setText(json.getString("text"));
        info.setStatus(json.getString("status"));

        ArrayList<MyList> mylists = new ArrayList<MyList>();

        JSONArray  panels = json.getJSONArray(????????????);
        for(int i=0;i < panels.length();i++){                          
            JSONObject e2 = panels.getJSONObject(i);
            MyList info_list = new MyList();

            info_list.setId(e2.getString("id"));
            info_list.setNumber(e2.getString("number"));

            info_list.add(info_answer);
        }

        info.setList(info_list);

    } catch (JSONException e) {

    } 
    return info;
}

请帮我。

推荐答案

是的,这是可能的。

把你的的JSONObject 收到JSON。您可以循环槽的并获得出来。

Put the JSON you receive in a JSONObject. You can loop trough the keys and get the values out of it.

示例:

//Create json object from string
JSONObject newJson = new JSONObject(json);

// Get keys from json
Iterator<String> panelKeys = newJson.keys();

while(panelKeys.hasNext()) {
     JSONObject panel = newJson.getJSONObject(panelKeys.next()); // get key from list
     String id  = panel.getString("id");
     String number  = panel.getString("number");
}

我希望这是你要找的人。

I hope this is what you were looking for

这篇关于如何解析JSON与Android上的任意键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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