简体   繁体   English

我有一个错误:类型 org.json.JSONObject 无法转换为 JSONArray

[英]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.嘿,伙计们,我正在使用 android 和 java 并尝试在我的代码上读取一些 json 数据,但我有一个错误:类型 org.ZAD6466DEEC1F6ECDF32FC45466DEEC1F6ECDF32FC45466DEEC7F6ECDF32FC454387 JSONObject cannot be converted to JSONArray JSONObject无法转换为JSONArray

I have JSON DATA in my web service response something like that:我的 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": []
}

Here is the code implemented to read this response ( json ): I'm using asyncTask and the error is in my onPostExecute method这是为读取此响应而实现的代码( 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 isn't an array so it's normal pradr不是数组,所以很正常

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:如果你想让这个工作改变你的 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"
                }],...

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:或者您可以将 java 中的代码更改为此(但请注意您将无法循环对象)也许您在代码中输入了错误的键:

JSONObject arr = new JSONObject(GST); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将org.json.JSONObject类型的数据转换为JSONArray - Data of type org.json.JSONObject cannot be converted to JSONArray 类型为org.json.JSONObject的响应无法转换为JSONArray - at response of type org.json.JSONObject cannot be converted to JSONArray at 类型 org.json.JSONObject 的数据无法转换为 JSONArray - at data of type org.json.JSONObject cannot be converted to JSONArray 值类型 org.json.JSONObject 无法转换为 JSONArray - Value type org.json.JSONObject cannot be converted to JSONArray org.json.JSONObject无法转换为JSONArray - org.json.JSONObject cannot be converted to JSONArray 错误org.json.JSONObject无法转换为JSONArray - error org.json.JSONObject cannot be converted to JSONArray 解析JSON数组时,获取异常“ org.json.JSONObject类型的答案无法转换为JSONArray” - Getting exception “ at answer of type org.json.JSONObject cannot be converted to JSONArray” while parsing JSON array 在Android中解析JSON响应“无法将org.json.JSONObject类型的响应转换为JSONArray” - Parse JSON response in android “response of type org.json.JSONObject cannot be converted to JSONArray” 错误:类型 org.json.JSONArray 无法转换为 JSONObject - error: type org.json.JSONArray cannot be converted to JSONObject org.json.JSONException:值 {“storeid0”:[“1535”],“storeid1”:[“1862”]} 类型为 org.json.JSONObject 的 idddsss 无法转换为 JSONArray - org.json.JSONException: Value {“storeid0”:[“1535”],“storeid1”:[“1862”]} at idddsss of type org.json.JSONObject cannot be converted to JSONArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM