java - wp :featuredmedia of type org. json.JSONArray 无法转换为 JSONObject

标签 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);
                       }

错误说法是:

org.json.JSONArray 类型的

wp:featuredmedia 无法转换为 JSONObject

我哪里错了。

最佳答案

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

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

关于java - wp :featuredmedia of type org. json.JSONArray 无法转换为 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52476532/

相关文章:

Android:如何使用 ndk 从 C++ 代码中读取游戏 Assets 文件

android - 如何查找手机是否处于 Android sleep /空闲模式

Java 转 JSON : Specify which fields get excluded for each conversion

javascript - 我如何使用 jQuery 一次性遍历嵌套的 JSON?

java - 如何从java中的文本文件向 map 插入数据

java - 尝试使用扫描器类循环函数

java - Mqtt-Spring Boot 中的集成

android - 记录由 Open GL ES 在 android 上生成的帧

java - 如何使用REST将Json字符串发送到android参数中的java webservice?

java - 在java中创建硬链接(hard link)