2011-03-16 75 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);