【问题标题】:JSON Parser: Error parsing data org.json.JSONException: type org.json.JSONArray cannot be convertedJSON 解析器:解析数据时出错 org.json.JSONException:无法转换类型 org.json.JSONArray
【发布时间】:2019-01-12 07:53:28
【问题描述】:
private class BackTask extends AsyncTask<Void, Void, Void> {
    protected void onPreExecute() {
        super.onPreExecute();
        dialog.show();
    }
    String url = "http://learnd.cf/getOtp.php";
    protected Void doInBackground(Void... params) {
        List<NameValuePair> list = new ArrayList<NameValuePair>();

        list.add(new BasicNameValuePair("username",USERNAME));
        list.add(new BasicNameValuePair("phone",PHONE));
        // getting JSON Object
        // Note that create datas url accepts POST method
        JSONObject json = (JSONObject) jsonParser.makeHttpRequest(url,"GET", list);

        // check log cat fro response
        Log.e("Create Response", json.toString());

        // check for success tag


        return null;
    }

    protected void onPostExecute(Void result) {
        dialog.dismiss();
    }
}

2019-01-12 13:09:36.585 11371-11569/com.example.rajeeshkv.learn_d E/JSON Parser:
    Error parsing data org.json.JSONException: Value 
    [  
        {  
            "temp_id":"12",
            "temp_clg_id":"1",
            "temp_course_id":"1",
            "temp_stud_name":"a",
            "temp_username":"a",
            "temp_password":"a",
            "temp_email":"a",
            "temp_phone":"4",
            "temp_gender":"Male",
            "temp_otp":"2060"
        }
    ]
    of type org.json.JSONArray cannot be converted to JSONObject

这是一个后台任务,将用户名和电话传递给数据库并从数据库中获取相应的行,然后使用 json 解析器将其传递回 android。但我收到以下错误。我已经尝试了很多来解决这个问题。有没有什么办法解决这一问题?

【问题讨论】:

  • 能否提供stacktrace?

标签: java android jsonparser


【解决方案1】:

我认为在获取请求中,您的用户名和电话应该在哈希图中而不是 BasicNameValuePair 列表中

【讨论】:

  • 我曾经从该 json 对象中打印一个值,并且冒号出现在它的前面。你能帮我解决这个问题吗?
【解决方案2】:

正如花括号 ([]) 所暗示的,您提供的 json 是一个 json 数组,而不是一个 json 对象。

试试这个

    JSONObject json = (JSONObject) ((JSONArray)jsonParser.makeHttpRequest(url,"GET", list)).getJSONObject(0);

【讨论】:

  • 我曾经从该 json 对象中打印一个值,并且冒号出现在它前面。你能帮我解决这个问题吗?
【解决方案3】:

2019-01-12 13:09:36.585 11371-11569/com.example.rajeeshkv.learn_d E/JSON 解析器:解析数据时出错 org.json.JSONException:值 [{"temp_id":"12"," temp_clg_id":"1","temp_course_id":"1","temp_stud_name":"a","temp_username":"a","temp_password":"a","temp_email":"a","temp_phone" org.json.JSONArray 类型的 :"4","temp_gender":"Male","temp_otp":"2060"}] 无法转换为 JSONObject

这意味着返回值是一个 JSONArray。但是您正在尝试将其转换为 JSONObject 这是不可能的。

试试

JSONArray jsonArray = (JSONArray) jsonParser.makeHttpRequest(url,"GET", list);
JSONObject json = jsonArray.get(0);

我使用jsonArray.get(0) 假设jsonArray 中只有一个对象。如果有更多项目,您必须使用 for 循环进行迭代以获取所有响应值。

【讨论】:

  • 我曾经从该 json 对象中打印一个值,并且冒号出现在它的前面。你能帮我解决这个问题吗?
  • 是的,我可以帮助你。您是如何尝试打印 json 对象的?你得到了哪个值?
  • JSONArray jArray = new JSONArray(result); JSONObject jsonObject = jArray.getJSONObject(0); otp = jsonObject.getString("temp_otp");
  • 在上面提供的代码中,我什么都做不了,所以我稍微改变了我的代码。现在我在 jArray 中获取 Json 数组
  • otp 的结果是什么?
【解决方案4】:

尝试使用

排球

库并以StringRequest 发送请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多