2011-03-16 77 views
0

我想解析JSON的網址,我的JSON的URL包含以下結構如何解析使用android的json url?

{"content":{"item":[{"category":"xxx"},{"category":"yy"} ]}} 

如何看這種結構,有人知道給例如一個JSON解析器。

謝謝

回答

1

此代碼將幫助您解析您的json。

String jsonStr = "{\"content\":{\"item\":[{\"category\":\"xxx\"},{\"category\":\"yy\"} ]}}"; 
try { 
     ArrayList<String> categories = new ArrayList<String>(); 
     JSONObject obj = new JSONObject(jsonStr); 
     JSONObject content = obj.getJSONObject("content"); 
     JSONArray array = content.getJSONArray("item"); 
     for(int i = 0, count = array.length();i<count;i++) 
     { 
      JSONObject categoty = array.getJSONObject(i); 
      categories.add(categoty.getString("category")); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
+0

由於帕沙,我下了放,精彩 – JohnRick 2011-03-16 14:11:37

0

您需要使用org.json的軟件包。 實施例:

對於對象:

JSONObject json = new JSONObject(json_string); 

對於數組:

JSONArray json = new JSONArray(json_string);