[WordPress] どのように WP API (JSON REST API WP プラグイン) を使用して返される JSON をパースするには

スポンサーリンク

質問

ちょうど JSON REST API ワードプレスのプラグイン ( プラグインのページへのリンク) によって生成される json データを解析する方法を理解しないでください。以下は、JSON 配列です人造人間とディスプレイで解析すると自分のサイトにこのプラグインによって作成されました。この配列は入れ子になっているので、この方法を知らないし、ルート要素に識別子/タグ/名前を持っていないしかし

重複しない: JSON REST API を使用して返される json はタグなしと異なる

JSON 配列部分

[{

    "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:\"&lt;\",\n nextText:\"&gt;\",\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&#038;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
        },
  }]

の答え

を使用しようとすると、解析するためのスニペットの下:

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");
}
}

http://stackoverflow.com/questions/29204389/how-to-parse-json-returned-using-the-wp-api-json-rest-api-wp-plugin

スポンサーリンク

Leave a Reply