安卓:JSON解析 [英] Android:json parsing

查看:175
本文介绍了安卓:JSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中,我不得不解析JSON以下工作,任何人都可以谁能告诉我如何做到这一点..

I am working on app in which i have to parse below json, anyone can anyone tell me how to do this..


  {\"navigation_entries\":{\"home\":{\"children\":[\"favorites\",\"calendar\",\"offers\",\"wineries\",\"dining\",\"lodging\",\"things_to_do\",\"weather\",\"settings\"],\"banner_image\":\"home_page_banner\",\"icon\":{\"small\":\"icon_home_small\",\"large\":\"icon_home_large\"},\"type\":\"home_page\",\"forward_icon\":{\"small\":\"icon_right_arrow_small\",\"large\":\"icon_right_arrow_large\"},\"link_name\":\"Home\"},\"wineries\":{\"display_name\":\"Wineries\",\"icon\":{\"small\":\"icon_wineries_small\",\"large\":\"icon_wineries_large\"},\"line_template\":\"wineries_list_template\",\"forward_icon\":{\"small\":\"icon_right_arrow_small\",\"large\":\"icon_right_arrow_large\"},\"link_name\":\"Wineries\",\"type\":\"leaf_list\",\"leaf_template\":\"wineries_leaf_template\",\"section\":\"wineries\"},\"dining\":{\"display_name\":\"Dining\",\"icon\":{\"small\":\"icon_dining_small\",\"large\":\"icon_dining_large\"},\"line_template\":\"dining_list_template\",\"forward_icon\":{\"small\":\"icon_right_arrow_small\",\"large\":\"icon_right_arrow_large\"},\"link_name\":\"Dining\",\"type\":\"leaf_list\",\"leaf_template\":\"dining_leaf_template\",\"section\":\"dining\"},\"offers_dining\":{\"display_name\":\"Offers => Dining\",\"list_name\":\"Dining\",\"line_template\":\"offers_dining_list_template\",\"type\":\"leaf_list\",\"leaf_template\":\"offers_dining_leaf_template\",\"forward_icon\":{\"small\":\"icon_right_arrow_small\",\"large\":\"icon_right_arrow_large\"},\"section\":\"offers_dining\"},\"favorites\":{\"display_name\":\"Favorites\",\"icon\":{\"small\":\"icon_favorites_small\",\"large\":\"icon_favorites_large\"},\"type\":\"favorites\",\"forward_icon\":{\"small\":\"icon_right_arrow_small\",\"large\":\"icon_right_arrow_large\"},\"link_name\":\"Favorites\"},\"offers\":{\"display_name\":\"Offers\",\"children\":[\"offers_wineries\",\"offers_dining\",\"offers_lodging\",\"offers_things_to_do\"],\"icon\":{\"small\":\"icon_offers_small\",\"large\":\"icon_offers_large\"},\"type\":\"navigation_list\",\"forward_icon\":{\"small\":\"icon_right_arrow_small\",\"large\":\"icon_right_arrow_large\"},\"link_name\":\"Offers\"}},\"type\":\"navigation\"}

show {"navigation_entries":{"home":{"children":["favorites","calendar","offers","wineries","dining","lodging","things_to_do","weather","settings"],"banner_image":"home_page_banner","icon":{"small":"icon_home_small","large":"icon_home_large"},"type":"home_page","forward_icon":{"small":"icon_right_arrow_small","large":"icon_right_arrow_large"},"link_name":"Home"},"wineries":{"display_name":"Wineries","icon":{"small":"icon_wineries_small","large":"icon_wineries_large"},"line_template":"wineries_list_template","forward_icon":{"small":"icon_right_arrow_small","large":"icon_right_arrow_large"},"link_name":"Wineries","type":"leaf_list","leaf_template":"wineries_leaf_template","section":"wineries"},"dining":{"display_name":"Dining","icon":{"small":"icon_dining_small","large":"icon_dining_large"},"line_template":"dining_list_template","forward_icon":{"small":"icon_right_arrow_small","large":"icon_right_arrow_large"},"link_name":"Dining","type":"leaf_list","leaf_template":"dining_leaf_template","section":"dining"},"offers_dining":{"display_name":"Offers => Dining","list_name":"Dining","line_template":"offers_dining_list_template","type":"leaf_list","leaf_template":"offers_dining_leaf_template","forward_icon":{"small":"icon_right_arrow_small","large":"icon_right_arrow_large"},"section":"offers_dining"},"favorites":{"display_name":"Favorites","icon":{"small":"icon_favorites_small","large":"icon_favorites_large"},"type":"favorites","forward_icon":{"small":"icon_right_arrow_small","large":"icon_right_arrow_large"},"link_name":"Favorites"},"offers":{"display_name":"Offers","children":["offers_wineries","offers_dining","offers_lodging","offers_things_to_do"],"icon":{"small":"icon_offers_small","large":"icon_offers_large"},"type":"navigation_list","forward_icon":{"small":"icon_right_arrow_small","large":"icon_right_arrow_large"},"link_name":"Offers"}},"type":"navigation"}

`

推荐答案

您可以使用本网站格式化在你的JSON字符串中一个简单的方法来查看。
Android提供了一个适当的库做你想要的东西。

You can use this website to format your JSON string in a in an easier way to view it. Android provides an appropiate library to do what you want.

是你需要COM $手册页p $ phend如何使用Android JSON API。

This is the manual page you need to comprehend how to use Android JSON API.

这里你可以看到一个教程,理解如何解析JSON字符串。

And here you can see a tutorial to understand how to parse JSON string.

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

如果上面的例子是你的字符串,可以解析它传递给JSONObject的构造器:

If the example above is your string, you can parse it passing it to the JSONObject constructor:

String jsonString = "...."; // the above string
try {
    JSONObject obj = new JSONObject(jsonString);
} catch (JSONException e) {
    // This exception is thrown if jsonString is not correctly formatted
}

现在,如果你想获得的JSONObject标有GlossList,里面上面的字符串,你可以这样做:

Now, if you want to get the JSONObject labled "GlossList", inside the string above, you can do this:

JSONObject glossList = obj.getJSONObject("glossary").getJSONObject("GlossDiv").getJSONObject("GlossList");

也有另一种可能:你也可以得到一些JSONArray。在我们的例子中,数组GlossSeeAlso:

There is also another possibility: you can also obtain some JSONArray. In our example, the array GlossSeeAlso:

JSONArray glossSee = glossList.getJSONObject("GlossEntry").getJSONObject("GlossDef").getJSONArray("GlossSeeAlso");

要直接拿到这个数组的值,可以使用方法的getString(int i)以;如果数组的抗衡是一个JSONObject,你可以使用的方法 getJSONObject(int i)以。您也可以使用该方法的getString(串标贴),将直接获得一个JSONObject的字符串:

To get directly a value of this array, you can use the method getString(int i); if the contend of the array is a JSONObject, you can use the method getJSONObject(int i). You can also use the method getString(String lable) to get directly a string in a JSONObject:

String title = glossDive.getString("title");
String firstElement = glossSee.getString(0);

其他的例子是在href=\"http://json.org/example.html\" rel=\"nofollow\">官方JSON网站的

这篇关于安卓:JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆