【发布时间】:2021-10-08 07:12:18
【问题描述】:
我是 kotlin 和 JSON 解析的新手,我无法让它正常工作kotlin 它给了我一个不匹配,我不确定输出的问题是什么,也许是方括号?有没有办法摆脱它们以便它可以读取它,或者我可以在 kotlin 方面做些什么?
private fun retriveProperties() {
val sharedPref = getSharedPreferences("#####", Context.MODE_PRIVATE)
var username = sharedPref.getString("user", null)
val password = sharedPref.getString("pas", null)
val url = "https://www.#########.com/api/getproperties.php?user="+username+"&pass="+password
val request = JsonObjectRequest(Request.Method.GET, url, null, Response.Listener {
response ->try {
val jsonArray = response.getJSONArray("")
for (i in 0 until jsonArray.length()) {
val employee = jsonArray.getJSONObject(i)
val firstName = employee.getString("FName")
Toast.makeText(applicationContext, firstName, Toast.LENGTH_LONG).show()
}
} catch (e: JSONException) {
e.printStackTrace()
}
}, Response.ErrorListener { error -> error.printStackTrace() })
requestQueue?.add(request)
}
堆栈跟踪是:
2021-08-02 21:52:02.322 19237-19237/com.#########.######### W/System.err: com.android.volley.ParseError: org.json.JSONException: Value [{"ID":"1","FName":"#########","LName":"#########","Email":"##################@#########.com","Username":"#########","Password#########","Tel":"#########","LotID":"1","Price":"1.0","Often":"BiWeekly","JLevel":"2","Subscription":"0","Active":"1","OrderID":"#########","PPemail":"##################@gmail.com","PPname":"######### #########","PPID":"#########"}] of type org.json.JSONArray cannot be converted to JSONObject
2021-08-02 21:52:02.322 19237-19237/com.#########.######### W/System.err: at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:90)
2021-08-02 21:52:02.323 19237-19237/com.#########.######### W/System.err: at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:144)
2021-08-02 21:52:02.323 19237-19237/com.#########.######### W/System.err: at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
2021-08-02 21:52:02.323 19237-19237/com.#########.######### W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)
2021-08-02 21:52:02.323 19237-19237/com.#########.######### W/System.err: Caused by: org.json.JSONException: Value [{"ID":"1","FName":"#########","LName":"#########","Email":"##################@#########.com","Username":"#########","Password#########","Tel":"#########","LotID":"1","Price":"1.0","Often":"BiWeekly","JLevel":"2","Subscription":"0","Active":"1","OrderID":"#########","PPemail":"##################@gmail.com","PPname":"######### #########","PPID":"#########"}] of type org.json.JSONArray cannot be converted to JSONObject
2021-08-02 21:52:02.323 19237-19237/com.#########.######### W/System.err: at org.json.JSON.typeMismatch(JSON.java:112)
我在服务器端尝试了trim($json, '[]'),但没有奏效,因为 kotlin 抱怨它不是真正的 JSON……所以现在正在寻找其他解决方案……
【问题讨论】:
标签: android arrays json kotlin parsing