У меня есть json {user_ip: "XXX.XXX.XXX.XXX"}
при разборе json, появляется следующая ошибка
type java.lang.String cannot be converted to JSONObject
Вот мой код
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
try {
val instance: JSONObject = JSONObject(response)
val asu = instance.getJSONObject("user_ip")
Log.d("parse_json", asu.toString())
} catch (e: JSONException) {
Log.d("error_json", e.message)
}
Запрос на получение URL был Content-Type: application / json;charset = UTF-8
{"user_ip":"XXX.XXX.XXX.XXX"}
я попробуйте ручной журнал
val response2 = response
val response3 = "{user_ip:\"1.1.1.1\"}"
Log.d("response2",response2)
Log.d("response3",response3)
D/response2: "{\"user_ip\":\"XXX.XXX.XXX.XXX\"}"
D/response3: {user_ip:"1.1.1.1"}
код работает с форматной переменной response3
val response3 = "{user_ip:\"1.1.1.1\"}"
спасибо