如何使用Android代码从JSON响应中获取特定值?

时间:2014-11-10 05:48:17

标签: android json

我需要获取特定对象值(A,B,C,D)和相关键值(@"name" )。获取(A, B, C, D )个对象值后,我需要列出Android列表视图。我在下面发布了示例代码和响应。请帮帮我。

@Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    contacts = jsonObj.getJSONArray("response");
                    Log.d("Response: ", "> " + contacts);

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

我的JSON回复:

{"response" : [  {

                "A"   :  [   {  
                         "name" : "tango" 
                           },
                       {
                        "name" : "ping"
                       }
                     ],

                "B"  :  [   {  
                         "name" : "tango" 
                           },
                       {
                        "name" : "ping"
                       }
                    ]
             } ]}

3 个答案:

答案 0 :(得分:3)

使用JSONObject keys()获取密钥,然后迭代每个密钥以获得动态值。

你可以获得像这样的动态键

JSONObject responseDataObj = new JSONObject(responseData);
JSONArray responseArray = responseDataObj.getJSONArray("response");
for (int i = 0; i < responseArray.length(); i++) {
    nodes = new ArrayList<ArrayList<String>>();//nodes ArrayList<ArrayList<String>> declared globally
    nodeSize = new ArrayList<Integer>();//nodeSize ArrayList<Integer> declared globally
    JSONObject obj = responseArray.getJSONObject(i);
    Iterator keys = obj.keys();
    while(keys.hasNext()) {
       // loop to get the dynamic key
       String currentDynamicKey = (String)keys.next();
       //store key in an arraylist which is A,B,...
       // get the value of the dynamic key
       JSONArray currentDynamicValue = obj.getJSONArray(currentDynamicKey);
       int jsonrraySize = currentDynamicValue.length();
       int sizeInArrayList = jsonrraySize + 1;
       nodeSize.add(sizeInArrayList);
       if(jsonrraySize > 0) {
           for (int ii = 0; ii < jsonrraySize; ii++) {
                nameList = new ArrayList<String>();//nameList ArrayList<String> declared globally
               if(ii == 0) {
                JSONObject nameObj = currentDynamicValue.getJSONObject(ii);
                String name = nameObj.getString("name");
                System.out.print("Name = " + name);
                //store name in an arraylist
                nameList.add(name);
              }
           }                    
       }
     nodes.add(nameList);
    }
}

答案 1 :(得分:0)

您可以使用以下方法来解析响应并根据需要处理输出。

private void parseJson(String res) {
    try {
        JSONObject mainObject = new JSONObject(res);
        JSONArray response = mainObject.getJSONArray("response");
        for (int i = 0; i < response.length(); i++) {
            JSONObject obj = response.getJSONObject(i);

            JSONArray A = obj.getJSONArray("A");

            for (int j = 0; j < A.length(); j++) {
                JSONObject objA = A.getJSONObject(j);
                String name = objA.getString("name");
                // use or store name here
            }

            JSONArray B = obj.getJSONArray("B");

            for (int k = 0; k < B.length(); k++) {
                JSONObject objB = B.getJSONObject(k);
                String name = objB.getString("name");
                // use or store name here
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

答案 2 :(得分:0)

这可能对您有用:

        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
        Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    JSONArray contacts;
                    contacts = jsonObj.getJSONArray("response");
                    Log.d("Response: ", "> " + contacts);

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);
                        JSONArray jsonArrayA=new JSONArray();
                        jsonArrayA=c.getJSONArray("A");
                        for(int j=0;j<jsonArrayA.length();j++){
                            String name=jsonArrayA.getJSONObject(j).getString("name");
                            Log.e("Name","name of jsonarray A "+i+" "+name);
                        }

                        JSONArray jsonArrayB=c.getJSONArray("B");
                        for(int k=0;k<jsonArrayB.length();k++){
                            String name=jsonArrayB.getJSONObject(k).getString("name");
                            Log.e("Name","name of jsonarray B "+i+" "+name);
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }