我只是不明白如何解析JSON REST API WordPress插件(Link to Plugin Page)生成的json数据。下面是我的网站上这个插件创建的JSON数组,我想在android和显示中解析它。但我不知道如何做到这一点,因为这个数组是嵌套的,并且没有对根元素的标识符/标记/名称。
NOT DUPLICATE:使用JSON REST API返回的json没有标记且不同。
JSON ARRAY部分
[{
"ID": 265,
"title": "Prince William Distressed Homes",
"status": "publish",
"type": "portfolio",
"author":
{
"ID": 1,
"username": "MrDarpan",
"name": "MrDarpan",
"first_name": "",
"last_name": "",
"nickname": "MrDarpan",
"slug": "MrDarpan",
"URL": "",
"avatar": "http://1.gravatar.com/avatar/fa87ce7d0b7411886af9a4a16c134075?s=96",
"description": "",
"registered": "2015-03-15T02:47:06+00:00",
"meta":
{
"links":
{
"self": "http://darpankulkarni.in/wp-json/users/1",
"archives": "http://darpankulkarni.in/wp-json/users/1/posts"
}
}
},
"content": "<!-- meta slider -->\n<div style=\"max-width: 682px; margin: 0 auto;\" class=\"metaslider metaslider-flex metaslider-268 ml-slider nav-hidden\">\n \n <div id=\"metaslider_container_268\">\n <div id=\"metaslider_268\">\n <ul class=\"slides\">\n <li style=\"display: block; width: 100%;\" class=\"slide-266 ms-image\"><img src=\"http://darpankulkarni.in/wp-content/uploads/2015/03/Prince-William-Distressed-Homes-1.jpg\" height=\"459\" width=\"682\" alt=\"Prince William Distressed Homes\" class=\"slider-268 slide-266\" title=\"Prince William Distressed Homes\" /><div class=\"caption-wrap\"><div class=\"caption\">Prince William Distressed Homes Landing Page by Darpan Kulkarni</div></div></li>\n <li style=\"display: none; width: 100%;\" class=\"slide-267 ms-image\"><img src=\"http://darpankulkarni.in/wp-content/uploads/2015/03/Prince-William-Distressed-Homes-2.jpg\" height=\"459\" width=\"682\" alt=\"Prince William Distressed Homes\" class=\"slider-268 slide-267\" title=\"Prince William Distressed Homes\" /><div class=\"caption-wrap\"><div class=\"caption\">Prince William Distressed Homes Landing Page by Darpan Kulkarni</div></div></li>\n </ul>\n </div>\n \n </div>\n <script type=\"text/javascript\">\n var metaslider_268 = function($) {\n $('#metaslider_268').addClass('flexslider'); // theme/plugin conflict avoidance\n $('#metaslider_268').flexslider({ \n slideshowSpeed:3000,\n animation:\"fade\",\n controlNav:false,\n directionNav:true,\n pauseOnHover:true,\n direction:\"horizontal\",\n reverse:false,\n animationSpeed:600,\n prevText:\"<\",\n nextText:\">\",\n slideshow:true\n });\n };\n var timer_metaslider_268 = function() {\n var slider = !window.jQuery ? window.setTimeout(timer_metaslider_268, 100) : !jQuery.isReady ? window.setTimeout(timer_metaslider_268, 1) : metaslider_268(window.jQuery);\n };\n timer_metaslider_268();\n </script>\n</div>\n<!--// meta slider-->\n",
"parent": 0,
"link": "http://darpankulkarni.in/portfolio/prince-william-distressed-homes/",
"date": "2015-03-21T05:36:15+00:00",
"modified": "2015-03-21T05:36:15+00:00",
"format": "standard",
"slug": "prince-william-distressed-homes",
"guid": "http://darpankulkarni.in/?post_type=portfolio&p=265",
"excerpt": null,
"menu_order": 0,
"comment_status": "closed",
"ping_status": "closed",
"sticky": false,
"date_tz": "UTC",
"date_gmt": "2015-03-21T05:36:15+00:00",
"modified_tz": "UTC",
"modified_gmt": "2015-03-21T05:36:15+00:00",
"meta":
{
"links":
{
"self": "http://darpankulkarni.in/wp-json/posts/265",
"author": "http://darpankulkarni.in/wp-json/users/1",
"collection": "http://darpankulkarni.in/wp-json/posts",
"replies": "http://darpankulkarni.in/wp-json/posts/265/comments",
"version-history": "http://darpankulkarni.in/wp-json/posts/265/revisions"
}
},
{
//More items in array
},
}]
答案 0 :(得分:0)
尝试使用以下代码段进行解析:
public parseJson(String jsonMessage){
JSONParser jsonParser = new JSONParser();
// Setting values in objects..
Object object = jsonParser.parse(jsonMessage);
JSONArray jsonArray = (JSONArray) object;
Iterator j = jsonArray.iterator();
while (j.hasNext()) {
JSONObject innerObj;
Object value = j.next();
if(value instanceof JSONObject){
innerObj = (JSONObject)value;
String id = (String) innerObj.get("ID");
String title = (String) innerObj.get("title");
String status = (String) innerObj.get("status");
String type = (String) innerObj.get("type");
JSONObject author= (JSONObject) innerObj.get("author");
String authorId = (String)author.get("authorId");
String username = (String)author.get("username");
String name = (String)author.get("name");
String firstName = (String)author.get("firstName");
String lastName = (String)author.get("lastName");
String nickname = (String)author.get("nickname");
String authorSlug = (String)author.get("slug");
String URL = (String)author.get("URL");
String avatar = (String)author.get("avatar");
String description = (String)author.get("description");
String registered = (String)author.get("registered");
JSONObject meta= (JSONObject) author.get("meta");
JSONObject links= (JSONObject) meta.get("links");
String self = (String)links.get("self");
String archives = (String)links.get("archives");
String parent= (String) innerObj.get("parent");
String link= (String) innerObj.get("link");
String date= (String) innerObj.get("date");
String modified= (String) innerObj.get("modified");
String format= (String) innerObj.get("format");
String slug= (String) innerObj.get("slug");
String guid= (String) innerObj.get("guid");
String excerpt= (String) innerObj.get("excerpt");
String menu_order= (String) innerObj.get("menu_order");
String comment_status= (String) innerObj.get("comment_status");
String ping_status= (String) innerObj.get("ping_status");
String sticky= (String) innerObj.get("sticky");
String date_tz= (String) innerObj.get("date_tz");
String date_gmt= (String) innerObj.get("date_gmt");
String modified_tz= (String) innerObj.get("modified_tz");
String modified_gmt= (String) innerObj.get("modified_gmt");
}
}