Android 如何解析这个JSON字符串?
我正在开发一个应用程序,我必须解析下面的JSON 有人能告诉我怎么做吗Android 如何解析这个JSON字符串?,android,android-json,Android,Android Json,我正在开发一个应用程序,我必须解析下面的JSON 有人能告诉我怎么做吗 { "navigation_entries": { "home": { "children": [ "favorites", "calendar", "offers", "wineries", "dining",
{
"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"
}
您可以这样使用和:
String json = "{"
+ " \"query\": \"Pizza\", "
+ " \"locations\": [ 94043, 90210 ] "
+ "}";
JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
String query = object.getString("query");
JSONArray locations = object.getJSONArray("locations");
您可以使用将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。在我们的示例中,数组glossSee还包括:
JSONArray glossSee = glossList.getJSONObject("GlossEntry").getJSONObject("GlossDef").getJSONArray("GlossSeeAlso");
要直接获取此数组的值,可以使用方法getString(inti)
;如果数组的内容是JSONObject,则可以使用方法getJSONObject(inti)
。您还可以使用方法getString(stringlable)
直接获取JSONObject中的字符串:
String title = glossDive.getString("title");
String firstElement = glossSee.getString(0);
其他示例可以在中找到。jsonlint.com有一个非常好的json格式化程序。这会使你的文章容易理解
要解析此数据,可以使用JSONObject(org.json.JSONObject)好的格式是您的朋友。上面提供的json字符串无效。请提供正确的Json字符串。您可以使用以下链接检查其有效性:上面的JSON字符串无效,因为开头有“show”标签可能与Vito Shadow重复,请告诉我如何在android中使用google图表和Fusion图表。如果你喜欢这个答案,你能接受我的答案吗?我从来没有用过谷歌和福森图表。。。对不起,我帮不了你。。。试着打开另一个问题,有人会帮助你;)