2015-10-27 81 views
0

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

我期待:

----------------------------------------- 
    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" 
} 
+0

欢迎。你能在你的文章中描述什么不起作用吗? – wwkudu

+0

嗨。一切工作正常。我需要从上面的响应中获得特定的值,并在列表视图中列出。 – SteaveJobs

+0

好的。你试过什么了?你卡在哪里? – wwkudu

回答

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");

+0

好吧,我会尝试!谢谢!顺便说一句,我需要列出所有A B C值。不仅是“A”。此外,如果我把上面的代码,我得到错误! – SteaveJobs

+0

哪个错误。 并且同样尝试使用String b = json.get(“B”)。toString(); – Gaurav

+0

首先感谢你的回复。我需要获取所有JSONObject,而不仅仅是“A”。为什么因为我会列出一些值到LIstView! – SteaveJobs

0

为了得到从JSON对象的字符串,你可以使用下面的

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

A型的男生

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

类型的女孩

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

运行的follwoing线在一个循环中,并根据您的需要

+0

我不能提“A”和“B”和“C”,因为可能它会高达Z.不提对象值,如何让所有的值和上市了! – SteaveJobs

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... 
    } 
相关问题