Это журнал ошибок в студии Android.

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

Где я ошибаюсь.

0
Anonymous 24 Сен 2018 в 12:42

2 ответа

Лучший ответ

Вместо этого вам следует использовать .getJSONArray("wb:featuremedia"). попробуй это

    String imgURL = post.getJSONObject("_embedded").
                    getJSONArray("wp:featuredmedia").
                    getJSONObject(0).
                    getJSONObject("media_details").
                    getString("file");
1
Ravindra Kushwaha 26 Сен 2018 в 06:25

Вы разбираете неправильный 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();
    }

ИЛИ , вы можете получить конкретный индекс из JSONArray, как показано ниже

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

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

        } catch (Exception ex) {
            ex.printStackTrace();
        }
1
Ravindra Kushwaha 24 Сен 2018 в 10:08