开发者

How to obtain data from Json in android?

In my app I need to obtain some data from server. In eclipse I receive data like this :

[{"id_category":"1","category_name":"Cool"},{"id_category":"2","category_name":"Glamor"},{"id_category":"3","category_name":"Funky"},{"id_category":"4","category_name":"Crazy"},{"id_category":"5","category_name":"David Guetta"}] without any name.

I tried something like this :

if (response != null) {
                responseBody = EntityUtils.toString(response.getEntity());
                System.out
                        .println("Rezultat de la server pentru a afla categoriile:"
                                + responseBody);
try {
                    final String r = response.toString();
String name = json.getString("category name");
System.out.println("One category"+name);
                } catch (JSONException e) {
                    // do nothing
                } catch (FacebookError e) {
                开发者_开发百科    e.getMessage();
                }

but nothing happens. How can I get id_category and category_name from my response? Can anyone help me? Thanks in advance.


This is how you parse JSON...I hope it works.

    JSONArray jArray = new JSONArray(r);
    for(int i=0;i<jArray.length();i++){
            JSONObject jsdata = jArray.getJSONObject(i);
            String myString = jsdata.getString("id_category");
            String myString2 = jsdata.getString("category_name");
}


Try to use Google Gson https://sites.google.com/site/gson/gson-user-guide

It available for maven

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜