我正在使用app,我必须在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"
}
答案 0 :(得分:2)
您可以使用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
}
现在,如果你想让JSONObject标记为“GlossList”,在上面的字符串中,你可以这样做:
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中提供了其他示例。
答案 1 :(得分:0)
您可以像这样使用JSONObject和JSONTokener:
String json = "{"
+ " \"query\": \"Pizza\", "
+ " \"locations\": [ 94043, 90210 ] "
+ "}";
JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
String query = object.getString("query");
JSONArray locations = object.getJSONArray("locations");
答案 2 :(得分:0)
jsonlint.com有一个非常好的json格式化程序。这应该使您的文字易于理解。
用于解析此数据,您可以使用JSONObject(org.json.JSONObject)