简体   繁体   中英

I have an error : type org.json.JSONObject cannot be converted to JSONArray

Hey guys im using android and java and trying to read some json data on my code but i had an error: type org.json. JSONObject cannot be converted to JSONArray

I have JSON DATA in my web service response something like that:

{
"status_code": 1,
"gstin": "33 AVFC T7665 1TT",
"fetch Time": "2020-04-19T16:54:49.330",
"name": "A INDUSTRIES (I) PRIVATE LIMITED",
"trade name": " INDIA PRIVATE LIMITED",
"registration Date": "2017-07-01",
"center": "RANGE-III",
"state": "SALT LAKE",
"center_cd": "WA07031536",
"state_cd": "WB0856357",
"constitution": "Private Limited Company",
"type": "Regular",
"status": "Active",
"last Update Date": "2018-04-16",
"cancellation Date": null,
"nature": [
    "Wholesale Business",
    "Others"
],
"pradr": {
    "bnm": "ACHS - 7/6412/YR-C",
    "st": "A CHECK POST",
    "loc": "PALPURAN",
    "bno": "A TRADING CO.",
    "stcd": "West Bengal",
    "flno": "",
    "lt": "",
    "lg": "",
    "pncd": "7534643445",
    "ntr": "Wholesale Business, Others"
},
"adadr": []
}

Here is the code implemented to read this response ( json ): I'm using asyncTask and the error is in my onPostExecute method

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

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

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

    EditText editText;
    TextView resultTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.editText);
        resultTextView = findViewById(R.id.resultTextView);
    }

    public void getWeather(View view) {
        try {
            DownloadTask task = new DownloadTask();
            String gstin = URLEncoder.encode(editText.getText().toString(), "UTF-8");

            task.execute(www.dddd...............................);

            InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"Could not find weather :(",Toast.LENGTH_SHORT).show();
        }
    }

    public class DownloadTask extends AsyncTask<String,Void,String> {

        @Override
        protected String doInBackground(String... urls) {
            String result = "";
            URL url;
            HttpURLConnection urlConnection = null;

            try {

                url = new URL(urls[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                InputStream in = urlConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(in);
                int data = reader.read();

                while (data != -1) {
                    char current = (char) data;
                    result += current;
                    data = reader.read();
                }

                return result;

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

                Toast.makeText(getApplicationContext(),"Could not find weather :(",Toast.LENGTH_SHORT).show();

                return null;
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.i("JSON",s);

             try {
                JSONObject jsonObject = new JSONObject(s);

                String GST = jsonObject.getString("pradr");

                Log.i("GST Details", GST);

                JSONArray arr = new JSONArray(GST);

                String message = "";

                for (int i=0; i < arr.length(); i++) {
                    JSONObject jsonPart = arr.getJSONObject(i);

                    String main = jsonPart.getString("loc");
                    String description = jsonPart.getString("bno");

                    if (!main.equals("") && !description.equals("")) {
                        message += main + ": " + description + "\r\n";
                    }
                }

                if (!message.equals("")) {
                    resultTextView.setText(message);
                } else {
                    Toast.makeText(getApplicationContext(),"Could not find weather :(",Toast.LENGTH_SHORT).show();
                }

            } catch (Exception e) {

                Toast.makeText(getApplicationContext(),"Could not find weather :(",Toast.LENGTH_SHORT).show();

                e.printStackTrace();
            }

        }
    }
}```









pradr isn't an array so it's normal

you canno't do this:

JSONArray arr = new JSONArray(GST); 

to this:

 ..."pradr": {
            "bnm": "ACHS - 7/6412/YR-C",
            "st": "A CHECK POST",
            "loc": "PALPURAN",
            "bno": "A TRADING CO.",
            "stcd": "West Bengal",
            "flno": "",
            "lt": "",
            "lg": "",
            "pncd": "7534643445",
            "ntr": "Wholesale Business, Others"
        },...

if you want this to work change your json response to an array just like that:

       ..."pradr":[{
                "bnm": "ACHS - 7/6412/YR-C",
                "st": "A CHECK POST",
                "loc": "PALPURAN",
                "bno": "A TRADING CO.",
                "stcd": "West Bengal",
                "flno": "",
                "lt": "",
                "lg": "",
                "pncd": "7534643445",
                "ntr": "Wholesale Business, Others"
                }],...

Or you can change your code in java to this (but pay attention you won't be able to loop an object) Maybe you putted the wrong key on your code:

JSONObject arr = new JSONObject(GST); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM