从此JSON对象中检索值

时间:2017-07-01 05:51:47

标签: android json

我要解析此$ ./bin/initials Name: David C. Rankin DCR $ ./bin/initials Name: Jane Doe JD $ ./bin/initials Name: d D $ ./bin/initials Name: George W... Bush GWB 并获取用户的值: 名称基础

json

2 个答案:

答案 0 :(得分:2)

尝试这样的事情......

try {
           JSONArray jsonArray = new JSONArray(response);

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

                JSONObject jsonObject = jsonArray.getJSONObject(i);

               // String name = jsonObject.getString("")

                JSONObject jsonObject1_user = jsonObject.getJSONObject("user");

                JSONObject jsonObject1_avater = jsonObject1_user.getJSONObject("avatar");

                String base = jsonObject1_avater.getString("base");
                String name = jsonObject1_user.getString("name");

                Log.d("getbase_name","values  "+base+" "+name);

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

答案 1 :(得分:1)

从JsonArray中提取数据,试试这样的事情。

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

                         // Getting JSON Array node
                        JSONArray contacts = jsonObj.getJSONArray("response");

                        // Getting JSON User Object node
                        JSONObject userObj = contacts.getJSONObject("user");

            //Getting name key from user object                    
                         String name = userObj.getString("name");

                        //Getting avatar node from user object 
                        JSONObject avatarObj= userObj.getJSONObject("avatar");

                        //Getting base key from avatar object  
                        String base= avatarObj.getString("base");


                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });

                }
    }