【问题标题】:of type org.json.JSONObject cannot be converted to JSONArray a [closed]org.json.JSONObject 类型的无法转换为 JSONArray [关闭]
【发布时间】:2020-11-09 18:14:34
【问题描述】:

嗨,先生,我已经看到整个堆栈溢出,但没有发现 aswer 请帮助我,我从 api 获取 json 数据,但我无法从中获取一些项目,我多次收到此错误。如果有任何我错过的问题或解决方案,请帮我解决..

private class JsonTask extends AsyncTask<String, String, String> {

    protected void onPreExecute() {
        super.onPreExecute();

        pd = new ProgressDialog(MainActivity.this);
        pd.setMessage("Please wait");
        pd.setCancelable(false);
        pd.show();
    }

    protected String doInBackground(String... params) {


        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();


            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();
            String line = "";
            Log.e(TAG, "doInBackground: BUFFER" + reader.read());
            while ((line = reader.readLine()) != null) {
                buffer.append(line + "\n");
                Log.e(TAG, "> " + line);   //here u ll get whole response...... :-)
                Log.e(TAG, "doInBackground: length " + line);
            }

            return buffer.toString();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result1) {
        super.onPostExecute(result1);
        String result = result1.toString();
        if (pd.isShowing()) {
            pd.dismiss();
        }
        Log.e(TAG, "onPostExecute: " + result);

        try {

            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {

                JSONObject jObject = jArray.getJSONObject(i);
                String name = jObject.getString("name");
                Log.e(TAG, "onPostExecute: ");

            } // End Loop
            Log.e(TAG, "onPostExecute: jsion " + jArray.length());
        } catch (JSONException e) {
            Log.e(TAG, "onPostExecute Error: " + e.toString());
        }
    }
}

这里是用api调用的函数

        new JsonTask().execute("https://api.iextrading.com/1.0/ref-data/symbols");

【问题讨论】:

  • 我只想获得每个项目的名称和符号,请帮助我任何身体..
  • 这能回答你的问题吗? How to parse JSON in Java
  • 先生的问题是,api 只提供 json 数据,而不是像“post”等所示的一些响应,否则我会解决这个问题
  • “posts”:[ {“post_id”:“123456789012_123456789012”,“actor_id”:“1234567890”,“picOfPersonWhoPosted”:“example.com/photo.jpg”,“nameOfPersonWhoPosted”:“Jane Doe”,“ message": "听起来很酷。等不及要看了!", "likesCount": "2", "cmets": [], "timeOfPost": "1234567890" } ]
  • api 不会那样返回

标签: java android json api android-studio


【解决方案1】:

https://api.iextrading.com/1.0/ref-data/symbols 返回一个包含 9290 个 JSONObjects 的平面 JSONArray。

您发布的代码没有任何问题,它在我这边运行良好 - 我唯一要更改的是删除对 StringBuffer 的需求,并且您不需要附加“\n”.. JSON 读取为一行。

 String line = "";
 String returned = "";
 while ((line = reader.readLine()) != null) {
      returned = line;
 }
 return returned;

您是说您缺少一些数据 - 那是因为返回的数据缺少项目。您可以通过将完整的 JSONArray 返回字符串放入记事本++ 或其他可以轻松为您格式化数组的东西来查看这一点,您会看到某些 JSONObjects 缺少一些“名称”..

例如 - 对象编号 9241

symbol : ZIONP
name :
date : 2020-11-09
isEnabled : true
type : N/A
iexId : 7674

您正在尝试获取名称,但无法获取名称 - 无法获取数据。

【讨论】:

  • 当我想在执行后读取获取单个项目时出现问题
  • 请帮助我在后期执行中仅从对象的 jsonArray 中将其他数据作为符号读取。我支持你,因为你理解我并给我时间,我很感谢你
  • 更改字符串名称 = jObject.getString("name"); to String name = jObject.optString("name"); - 然后你可以检查名称是否为空,如果是,那么你可以 jObject.getString("symbol");
  • 先生,抱歉打扰您,但在执行后的捕获过程中遇到此错误 >>>>>>>>onPostExecute 错误:org.json.JSONException: Value {"symbol":"A"," name":"AGILENT TECHNOLOGIES INC","date":"2020-11-10","isEnabled":true,"type":"N\/A","iexId":"2"} 类型为 org。 json.JSONObject 无法转换为 JSONArray
  • 您是否更新了代码以删除 StringBuffer 而不是在该行中添加“\n”?
猜你喜欢
  • 1970-01-01
  • 2021-07-12
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
相关资源
最近更新 更多