gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:42 26 4
gpt4 key购买 nike

我在运行我的应用程序时收到 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com