类型为org.json.JSONArray的wp:featuredmedia无法转换为JSONObject

时间:2018-09-24 09:42:09

标签: java android json wp-api

这是Android Studio中的错误日志。

W/System.err: org.json.JSONException: Value [{"id":283,"date":"2017-12-04T14:24:03","slug":"one-plus-5t-starwars","type":"attachment","link":"http:\/\/url.in\/2017\/12\/04\/oneplus5t-star-wars-limited-edition-december-14\/one-plus-5t-starwars\/","title":{"rendered":"FEATURED"},"author":1,"caption":{"rendered":"<p><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n"},"alt_text":"","media_type":"image","mime_type":"image\/jpeg","media_details":{"width":728,"height":513,"file":"2017\/12\/one-plus-5t-starwars.jpg","sizes":{"thumbnail":{"file":"one-plus-5t-starwars-150x150.jpg","width":150,"height":150,"mime_type":"image\/jpeg","source_url":"https:\/\/i0.wp.com\/url.in\/wp-content\/uploads\/2017\/12\/one-plus-5t-starwars.jpg?resize=150%2C150"},"medium":{"file":"one-plus-5t-starwars-300x211.jpg","width":300,"height":211,"mime_type":"image\/jpeg","source_url":"https:\/\/i0.wp.com\/url.in\/wp-content\/uploads\/2017\/12\/one-plus-5t-starwars.jpg?fit=300%2C211"},"editorial-featured-medium":{"file":"one-plus-5t-starwars-420x307.jpg","width":420,"height":307,"mime_type":"image\/jpeg","source_url":"https:\/\/i0.wp.com\/url.in\/wp-content\/uploads\/2017\/12\/one-plus-5t-starwars.jpg?resize=420%2C307"},"editorial-featured-long":{"file":"one-plus-5t-starwars-300x443.jpg","width":300,"height":443,"mime_type":"image\/jpeg","source_url":"https:\/\/i0.wp.com\/url.in\/wp-content\/uploads\/2017\/12\/one-plus-5t-starwars.jpg?resize=300%2C443"},"editorial-block-medium":{"file":"one-plus-5t-starwars-464x290.jpg","width":464,"height":290,"mime_type":"image\/jpeg","source_url":"https:\/\/i0.wp.com\/url.in\/wp-content\/uploads\/2017\/12\/one-plus-5t-starwars.jpg?resize=464%2C290"},"editorial-block-thumb":{"file":"one-plus-5t-starwars-322x230.jpg","width":322,"height":230,"mime_type":"image\/jpeg","source_url":"https:\/\/i0.wp.com\/url.in\/wp-content\/uploads\/2017\/12\/one-plus-5t-starwars.jpg?resize=322%2C230"},"wp_review_small":{"file":"one-plus-5t-starwars-65x65.jpg","width":65,"height":65,"mime_type":"image\/jpeg","source_url":"https:\/\/i0.wp.com\/url.in\/wp-content\/uploads\/2017\/12\/one-plus-5t-starwars.jpg?resize=65%2C65"},"wp_review_large":{"file":"one-plus-5t-starwars-320x240.jpg","width":320,"height":240,"mime_type":"image\/jpeg","source_url":"https:\/\/i0.wp.com\/url.in\/wp-content\/uploads\/2017\/12\/one-plus-5t-starwars.jpg?resize=320%2C240"},"full":{"file":"one-plus-5t-starwars.jpg?fit=728%2C513","width":728,"height":513,"mime_type":"image\/jpeg","source_url":"https:\/\/i0.wp.com\/url.in\/wp-content\/uploads\/2017\/12\/one-plus-5t-starwars.jpg?fit=728%2C513"}},"image_meta":{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0","keywords":[]}},"source_url":"http:\/\/url.in\/wp-content\/uploads\/2017\/12\/one-plus-5t-starwars.jpg","_links":{"self":[{"attributes":[],"href":"http:\/\/url.in\/wp-json\/wp\/v2\/media\/283"}],"collection":[{"attributes":[],"href":"http:\/\/url.in\/wp-json\/wp\/v2\/media"}],"about":[{"attributes":[],"href":"http:\/\/url.in\/wp-json\/wp\/v2\/types\/attachment"}],"author":[{"attributes":{"embeddable":true},"href":"http:\/\/url.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"attributes":{"embeddable":true},"href":"http:\/\/url.in\/wp-json\/wp\/v2\/comments?post=283"}]}}] at wp:featuredmedia of type org.json.JSONArray cannot be converted to JSONObject
                  at org.json.JSON.typeMismatch(JSON.java:100)
                  at org.json.JSONObject.getJSONObject(JSONObject.java:613)
                  at com.example.anon.swiggy.MainActivity$1.onPostExecute(MainActivity.java:80)
                  at com.example.anon.swiggy.MainActivity$1.onPostExecute(MainActivity.java:50)
                  at android.os.AsyncTask.finish(AsyncTask.java:667)
                  at android.os.AsyncTask.-wrap1(AsyncTask.java)
                  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:163)
                  at android.app.ActivityThread.main(ActivityThread.java:6228)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
D/SlideFragment: onPostExecute: on line 121

这是我用来从API检索数据的代码部分。

JSONArray json = new JSONArray(s);
                   for (int i = 0; i < json.length(); i++) {
                       JSONObject post = json.getJSONObject(i);
                       String title = post.getJSONObject("title").getString("rendered");
                       String description = post.getJSONObject("content").getString("rendered");
                       String imgURL = post.getJSONObject("_embedded").getJSONArray("wp:featuredmedia").getJSONObject("0").getJSONObject("media_details").getString("file");
                       String imagUrl = "http://url.in/wp-content/uploads/" + imgURL;
                       Log.d(TAG, "onPostExecute: 80 "+ imagUrl);
                       ImageNames.add(title);
                       ImageDesc.add(description);
                       ImageUrls.add(imagUrl);
                       }

错误说法是:

类型为wp:featuredmedia

org.json.JSONArray无法转换为JSONObject

我错了。

2 个答案:

答案 0 :(得分:1)

您正在解析错误的JSON,因此,发生异常,请参考以下解决方案。

 try {
        JSONArray jsonArray = new JSONArray("YOUR_RESPONSE");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONObject media_details = jsonObject.getJSONObject("media_details");

            String file = media_details.getString("file");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

OR ,您可以像下面这样从JSONArray获取特定索引

    try { 
            JSONArray jsonArray = new JSONArray("YOUR_RESPONSE");

            String file = jsonArray.getJSONObject(0).getJSONObject("media_details").getString("file");

        } catch (Exception ex) {
            ex.printStackTrace();
        }

答案 1 :(得分:1)

您应该改用.getJSONArray("wb:featuremedia")。试试这个

    String imgURL = post.getJSONObject("_embedded").
                    getJSONArray("wp:featuredmedia").
                    getJSONObject(0).
                    getJSONObject("media_details").
                    getString("file");