android - 我如何解析这个 JSON 字符串?

标签 android android-json

我正在开发必须在 JSON 以下进行解析的应用。

谁能告诉我怎么做?

{
    "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"
}

最佳答案

您可以使用 this website以一种更容易查看的方式格式化您的 JSON 字符串。 Android 提供了一个合适的库来做你想做的事。

This是了解如何使用 Android JSON API 所需的手册页。

here您可以查看教程以了解如何解析 JSON 字符串。

{
    "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 构造函数:

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

现在,如果你想在上面的字符串中获取标记为“GlossList”的 JSONObject,你可以这样做:

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

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

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

要直接获取此数组的值,可以使用方法getString(int i);如果数组的对象是JSONObject,可以使用方法getJSONObject(int i)。您还可以使用方法 getString(String lable) 直接获取 JSONObject 中的字符串:

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

official JSON site 中提供了其他示例.

关于android - 我如何解析这个 JSON 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7926199/

相关文章:

android - 以编程方式更改 Gingerbread 中的 Actionbar 标题颜色

安卓 : References to a Context and memory leaks

android - Google TV 视频查看播放 YouTube rtsp 视频

java - 如何在Android中使用GSON获取具有特定值的JSON对象

android - 无法使用 Volley 将 JSONArray 转换为 JSONObject

android - AsyncTask 和 JSON - Android

android - float 操作按钮重力不起作用

java - 为什么我设置 this.getReadableDatabase() 后需要关闭数据库?

android-volley - 如何使用 android volley JsonObjectRequest 从 php 获取字符串响应?

java - 如何独立于嵌套的 JSON 对象检索数据,这些对象属于 android/java 中的同一组? ( body 更清晰/细节)