1

我正在开发必须在 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"
}
4

3 回答 3

2

您可以使用此网站以更简单的方式格式化您的 JSON 字符串以查看它。Android 提供了一个适当的库来做你想做的事。

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

在这里你可以看到一个教程来了解如何解析 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);

其他示例可在官方 JSON 站点中找到。

于 2011-10-28T07:29:19.557 回答
0

您可以像这样使用JSONObjectJSONTokener

String json = "{"
     + "  \"query\": \"Pizza\", "
     + "  \"locations\": [ 94043, 90210 ] "
     + "}";

JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
String query = object.getString("query");
JSONArray locations = object.getJSONArray("locations");
于 2011-10-28T07:22:27.237 回答
0

jsonlint.com 有一个非常好的 json 格式化程序。这应该使您的文本易于理解。

要解析此数据,您可以使用 JSONObject (org.json.JSONObject)

于 2011-10-28T07:47:24.360 回答