2014-09-01 108 views
-4

我想从PHP的MySQL在Android.Here特定用户的数据是我的URL(XXX // XXXXXXX/client_vendor_mgmt/subcategory.php?CAT_ID = 2), 这里是URL数据以JSON如何从Android中的JSON或URL获取特定数据?

[ 
    { 
     "subcat_id": "1", 
     "subcat_code": "cell1", 
     "subcat_name": "cell", 
     "cat_id": "2", 
     "subcat_created_date": "0000-00-00", 
     "subcat_isactive": "Y", 
     "subcat_updated_date": "0000-00-00" 
    }, 
    { 
     "subcat_id": "2", 
     "subcat_code": "cell2", 
     "subcat_name": "tv", 
     "cat_id": "2", 
     "subcat_created_date": "0000-00-00", 
     "subcat_isactive": "Y", 
     "subcat_updated_date": "0000-00-00" 
    } 
] 

我想获得“cat_id”的所有记录:“2”。如何获得此记录。感谢高级。

+1

你尝试谷歌 “如何在Android的JSON解析”? – MilapTank 2014-09-01 06:21:55

+0

您已经拥有所有记录,并且您想要显示这些数据,请使用cat_id == 2 – 2014-09-01 06:25:38

+0

? – 2014-09-01 06:25:59

回答

1

下面是代码

try { 
      JSONArray array=new JSONArray("yourJsonString"); 

      JSONArray sortedArray = new JSONArray(); 
      for(int i=0;i<array.length();i++) 
      { 
       JSONObject obj=array.getJSONObject(i); 
       if(obj.getString("cat_id").equalsIgnoreCase("2")) 
       { 
        sortedArray.put(obj); 
       } 
      } 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
+0

: - 在你的JSONArray数组=新的JSONArray(“yourJsonString”); =我在这个“yourJsonString”这个地方放了什么? – tazeen 2014-09-01 06:31:20

+0

你从webservice获得的数据 – 2014-09-01 06:32:30

0

使用json解析器库,如GsonJackson

0
 JSONArray ja=new JSONArray("yourResponsString"); 
     JSONObject jo=ja.getJSONObject(0); 
     String cat_id=jo.getString("cat_id"); 
     if(cat_id.equals("2")){ 

      // this is your object 
     } 
+0

可能需要导入:import org.json.JSONObject; import org.json.JSONArray; – 2014-09-01 06:27:09

0
JSONArray ja=new JSONArray("yourResponsString"); 
    for(int i=0;i<ja.length;i++) 
{ 
JSONObject obj = ja.getJSONObject(i); 
    String cat_id=obj.getString("cat_id"); 
    if(cat_id.equals("2")){ 

     // this is your object 
    } 
} 
+0

虽然这段代码可以解决这个问题,但[包括一个解释](http://meta.stackexchange.com/questions/114762/explaining-entirely- code-based-answers)有助于提高您的帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。 – 2018-03-03 08:27:59