如何检索以下JSON输出的数据?

时间:2012-05-16 09:05:33

标签: android json

{"Result": {
    "version": [
      "001","002","003","004","005"
    ],
 "Names": {
      "001": "NoName","002": "NoName","003": "Cupcake","004": "Donut","005": "Eclair"
    }
}}
  

以上显示的结果是我现在作为回应   我如何从上面检索相应的名称.Pls。任何人   帮助我解决这个问题。谢谢

2 个答案:

答案 0 :(得分:2)

请参阅Android Json Parsing

的此示例

你可以这样做,可以帮助你。

JSONObject mjsonObject = null;
        try {
            mjsonObject=new JSONObject(jsonString);
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        JSONArray mjsonArray;

        JSONObject mJsonObject2,jsonObjectNames;
        try{
            mJsonObject2= mjsonObject.getJSONObject("Result");
            mjsonArray=mJsonObject2.getJSONArray("version");
            jsonObjectNames=mJsonObject2.getJSONObject("Names");

            String[] versionName=new String[mjsonArray.length()];
            for(int i=0;i<mjsonArray.length();i++){
                if(!mjsonArray.isNull(i)){
                    versionName[i]=mjsonArray.getString(i);
                }
            }
            String [] versionString=new String[mjsonArray.length()];



        }catch(JSONException json){
            json.printStackTrace();
        }catch(NullPointerException excep){

        }

答案 1 :(得分:0)

hai参考这个你会得到一个想法

    try {
        resp = new JSONObject(shared);
        cities = new JSONArray(resp.getString("cities"));

        for (int i = 0; i < cities.length(); i++) {

            JSONObject jsonObject = cities.getJSONObject(i);
            id = jsonObject.getString("id");

            operators = jsonObject.getJSONArray("operators");

            if (id.equalsIgnoreCase(String.valueOf(srcid1))) {

                for (int j = 0; j < operators.length(); j++) {
                    JSONObject jsonObject1 = operators.getJSONObject(j);
                    oid = jsonObject1.getString("id");

                    vehicle = jsonObject1.getJSONArray("vehicles");

                    if (oid.equalsIgnoreCase(value)) {
                        Log.v("json", "msg");
                        for (int k = 0; k < vehicle.length(); k++) {

                            jsonObject2 = vehicle.getJSONObject(k);
                            vehiclename = jsonObject2.getString("name");
                            id = jsonObject2.getString("id");
                            int vehicleid = Integer.parseInt(id);
                            map = new HashMap<String, String>();
                            Log.v("vehi", "" + vehiclename);
                            Log.v("vehi", "" + vehicleid);
                            map.put("id", "" + id);
                            map.put("vehiclename", "" + vehiclename);
                            mylist.add(map);
                            Log.v("count", "" + mylist.toString());

                        }

                    }

                }

            }

        }