簡體   English   中英

我有一個錯誤:類型 org.json.JSONObject 無法轉換為 JSONArray

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

嘿,伙計們,我正在使用 android 和 java 並嘗試在我的代碼上讀取一些 json 數據,但我有一個錯誤:類型 org.ZAD6466DEEC1F6ECDF32FC45466DEEC1F6ECDF32FC45466DEEC7F6ECDF32FC454387 JSONObject無法轉換為JSONArray

我的 web 服務響應中有 JSON DATA 是這樣的:

{
"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": []
}

這是為讀取此響應而實現的代碼( json ):我正在使用asyncTask並且錯誤在我的onPostExecute方法中

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不是數組,所以很正常

你不能做這個:

JSONArray arr = new JSONArray(GST); 

對此:

 ..."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"
        },...

如果你想讓這個工作改變你的 json 對數組的響應,就像這樣:

       ..."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"
                }],...

或者您可以將 java 中的代碼更改為此(但請注意您將無法循環對象)也許您在代碼中輸入了錯誤的鍵:

JSONObject arr = new JSONObject(GST); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM