开发者

How to parse json url using android?

开发者 https://www.devze.com 2023-02-17 17:14 出处:网络
I want parse json url,my json url contains followin开发者_Go百科g structures {\"content\":{\"item\":[{\"category\":\"xxx\"},{\"category\":\"yy\"} ]}}

I want parse json url,my json url contains followin开发者_Go百科g structures

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

how to read this structure,anybody knows give example json parser for that.

Thanks


This code will help you to parse yours 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();
  }


JSONObject can do the parsing.


You need to use the org.json's package. Example:

For an object:

JSONObject json = new JSONObject(json_string);

For an array:

JSONArray json = new JSONArray(json_string);
0

精彩评论

暂无评论...
验证码 换一张
取 消