如何使用Android从Volley JSON响应数据中获取特定值?

时间:2015-10-27 07:01:39

标签: android json android-volley

我已成功将Volley library整合到我的android studio项目中并得到以下回复。现在我需要从下面的响应中获取特定值。我还需要GET (A, B, C, D, E,etc,.. )价值观和相关的男孩和女孩价值观。一旦我得到它,我需要在list view上列出如下模型。

我期待:

-----------------------------------------
   Title        Boys            Girls
-----------------------------------------
     A          1.5                3.5
-----------------------------------------
     B          1.5                3.5
-----------------------------------------

在我的代码下面:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Displays Home Screen
        setContentView(R.layout.activity_list);

        //displaying TextView
        txtDisplay = (TextView) findViewById(R.id.txtDisplay);

        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "MYURL";

        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                // TODO Auto-generated method stub
                txtDisplay.setText("Response => "+response.toString());
                System.out.println("Response => "+response.toString());
                findViewById(R.id.progressBar1).setVisibility(View.GONE);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub

            }
        });

        queue.add(jsObjRequest);
    }

在我的回复之下:

{ 
   "response":{ 
      "A":{ 
         "boys":1.5,
         "girls":"3.5",
      },
      "B":{ 
         "boys":1.5,
         "girls":"3.5",
      },
      "E":{ 
         "boys":1.5,
         "girls":"3.5",
      }
  },
   "schools":{ 
   },
   "distances":"172 KM"
}

3 个答案:

答案 0 :(得分:2)

使用以下代码获取值:

JsonObject json = response.get("response");
String a =json.get("A").toString();

同样找到其他值;

例如

当你得到

JsonObject json = response.get("response");
然后json将是:

{ 
      "A":{ 
         "boys":1.5,
         "girls":"3.5",
      },
      "B":{ 
         "boys":1.5,
         "girls":"3.5",
      },
      "E":{ 
         "boys":1.5,
         "girls":"3.5",
      }
  }

如果你想从这个json中获得A / B / C

jsonA = json.get("A");


          { 
             "boys":1.5,
             "girls":"3.5",
          }

如果你想从A中挽回男孩 那么

String boys = jsonA.get("boys").toString();
同样

json.get("B");

json.get("C");

答案 1 :(得分:0)

为了从json对象获取字符串,您可以使用以下

  JSONObject jObj=new JSONObject(response.toString());

适用于a型男孩

 String a = jObj.getJSONObject("response").getJSONObject("A").getString("boys").toString();

适用于a型女孩

 String b = jObj.getJSONObject("response").getJSONObject("A").getString("girls").toString();

在循环中运行以下行并根据您的需要存储它们

答案 2 :(得分:0)

我自己得到了,我所期待的。

JSONObject data = response.getJSONObject("response");
Iterator keys = data.keys();

while(keys.hasNext()) {
        // loop to get the dynamic key
        String currentDynamicKey = (String)keys.next();

        // get the value of the dynamic key
        JSONObject currentDynamicValue = data.getJSONObject(currentDynamicKey);

        // do something here with the value...
    }