java - 如何消除这个错误->Json解析错误: Value success at response of type java. lang.String无法转换为JSONArray

标签 java android json

我在运行我的应用程序时收到 JSON 解析错误

下面提到了我遇到错误的代码和 json url https://zactra.com/blackboard/teacher/auth/email/check/numankhan2754@gmail.com

           MyHttpHandler myhttp = new MyHttpHandler();
           String Newurl = url + "auth/email/check/"+email+"/";

            // call MyServiceCall method from Myhttphandler class

            String jsonstng = myhttp.MyServiceCall(Newurl);

            Log.e(TAG, "Response From URL: " + jsonstng);
            if (jsonstng != null) {
                try {
                    JSONObject jsonObject = new JSONObject(jsonstng);

                    //getting JSON array node
                    JSONArray jsonArray = jsonObject.getJSONArray("response");

                    // Looping through all data from json
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject loopjsonObject = jsonArray.getJSONObject(i);

                        myid = loopjsonObject.getString("response");

                        // tmp hash map for single contact
                        HashMap<String, String> mydata = new HashMap<>();

                        // adding each child node to HashMap key => value
                        mydata.put("response", myid);

                        mydatalist.add(mydata);


                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            Toast.makeText(StartMainActivity.this, "JSON parsing error", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(StartMainActivity.this, "Couldn't get json from server. Check LogCat for possible errors!", Toast.LENGTH_SHORT).show();
                    }
                });
        }

来自 URL 的响应:{"response":"success"}

Json解析错误:value success at response of type java.lang.String cannot be converted to JSONArray

最佳答案

{"response":"success"} is a JSONObject but you are treating it as JSONArray.

JSON 数组总是用 [] 包裹 示例:["a", "b", "c"] 或类似 JSONObjects 数组的内容。例如:[{"key":"value"}, {"key","value"}]。许多组合都是可能的,但最重要的是它应该以 []

开头

关于java - 如何消除这个错误->Json解析错误: Value success at response of type java. lang.String无法转换为JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58029489/

相关文章:

Java/泛型 : Return a list of extending a type?

android - 子类 SurfaceView 中的 findViewById 抛出 RuntimeException

android - 如何在android 2.3及更高版本中设置没有操作栏的holo UI主题

mysql - 如何使用通配符从mysql查询json数据

javascript - 使用 unicode 时 XMLHttpRequest 主体被 chop

java - 确定多边形是否在 map 边界内

java - 平衡 Java J2EE 服务器上 CPU/内存资源的线程使用

java - 64 位 Java 应用程序 : Is a 64-bit OS, 需要 64 位 JRE 和 64 位应用程序吗?

android - 使用 fragment 实现操作栏选项卡

javascript - d3.js奇怪的图形显示