of type org.json.JSONObject cannot be converted to JSONArray

-1

I am getting an error of type org.json.JSONObject cannot be converted to JSONArray. What is wrong with the code? Here are the data for android studio at the bottom. Thanks.

2019-08-08 15:20:47.259 25280-25404/com.example.json3 I/Adreno: PFP: 0x016ee155, ME: 0x00000000
2019-08-08 15:20:47.263 25280-25404/com.example.json3 I/OpenGLRenderer: Initialized EGL, version 1.4
2019-08-08 15:20:47.263 25280-25404/com.example.json3 D/OpenGLRenderer: Swap behavior 2
2019-08-08 15:20:47.268 25280-25404/com.example.json3 D/libGLESv1: STS_GLApi : DTS, ODTC are not allowed for Package : com.example.json3
2019-08-08 15:20:47.270 25280-25404/com.example.json3 D/OpenGLRenderer: eglCreateWindowSurface = 0x789e646be0
2019-08-08 15:20:47.424 25280-25280/com.example.json3 D/ViewRootImpl@41d176b[MainActivity]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1440, 2960) ci=Rect(0, 84 - 0, 168) vi=Rect(0, 84 - 0, 168) or=1
2019-08-08 15:20:47.424 25280-25280/com.example.json3 D/ViewRootImpl@41d176b[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
2019-08-08 15:20:47.430 25280-25280/com.example.json3 V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@3fd8912 nm : com.example.json3 ic=null
2019-08-08 15:20:47.430 25280-25280/com.example.json3 I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
2019-08-08 15:20:47.447 25280-25280/com.example.json3 V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@8a308e3 nm : com.example.json3 ic=null
2019-08-08 15:20:49.102 25280-25280/com.example.json3 D/ViewRootImpl@41d176b[MainActivity]: ViewPostIme pointer 0
2019-08-08 15:20:49.175 25280-25280/com.example.json3 D/ViewRootImpl@41d176b[MainActivity]: ViewPostIme pointer 1
2019-08-08 15:20:49.198 25280-25781/com.example.json3 D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
2019-08-08 15:20:49.202 25280-25781/com.example.json3 I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
2019-08-08 15:20:49.202 25280-25781/com.example.json3 I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
2019-08-08 15:20:49.236 25280-25781/com.example.json3 D/TcpOptimizer: TcpOptimizer-ON
2019-08-08 15:20:49.696 25280-25781/com.example.json3 W/System.err: org.json.JSONException: Value {"posts":[{"title":"  Ending (Shao Kahn) | Mortal Kombat  ","author":"demo2"}]} of type org.json.JSONObject cannot be converted to JSONArray

Main Activity

package com.example.json3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Button click;
    public  static TextView data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        click = findViewById(R.id.button);
        data = findViewById(R.id.fetchdata);

        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fetchData process = new fetchData();
                process.execute();
            }
        });
    }
}

FETCHDATA CLASS

package com.example.json3;

import android.os.AsyncTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Abhishek Panwar on 7/14/2017.
 */

public class fetchData extends AsyncTask<Void,Void,Void> {
    String data ="";
    String dataParsed = "";
    String singleParsed ="";
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL("https://api.myjson.com/bins/au02l");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while(line != null){
                line = bufferedReader.readLine();
                data = data + line;
            }

            JSONArray JA = new JSONArray(data);

            for(int i =0 ;i <JA.length(); i++)
            {
                JSONObject JO = (JSONObject) JA.get(i);
                singleParsed =  "title:" + JO.get("title") + "\n"+
                                "author:" + JO.get("author") + "\n";

                dataParsed = dataParsed + singleParsed +"\n" ;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        MainActivity.data.setText(this.dataParsed);
    }

ACTIVITY_MAIN XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/click_me"
       android:layout_marginBottom="10dp"
       android:id="@+id/button" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/button">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:hint="@string/fetch_text_here"
            android:padding="10dp"
            android:textSize="24sp"
            android:id="@+id/fetchdata"
            />

    </ScrollView>
android
asked on Stack Overflow Aug 8, 2019 by Thien Vu • edited Aug 8, 2019 by second

1 Answer

0

"{"posts":[{"title":" Ending (Shao Kahn) | Mortal Kombat ","author":"demo2"}]}" is JSONObject , and it's posts attribute is JSONArray

JSONArray JA = new JSONObject(data).get("posts");

        for(int i =0 ;i <JA.length(); i++)
        {
            JSONObject JO = (JSONObject) JA.get(i);
            singleParsed =  "title:" + JO.get("title") + "\n"+
                            "author:" + JO.get("author") + "\n";

            dataParsed = dataParsed + singleParsed +"\n" ;
        }
answered on Stack Overflow Aug 8, 2019 by taha • edited Aug 8, 2019 by taha

User contributions licensed under CC BY-SA 3.0