[英]org.json.JSONObject creating JSONObject from string threw java.lang.RuntimeException
I've been trying to figure out why constructing a org.json.JSONObject
from a string json would not work for very long.我一直在试图弄清楚为什么从字符串 json 构造org.json.JSONObject
不会工作很长时间。
Here is the json string (pretty)这是 json 字符串(漂亮)
{
"status": 200,
"message": "200 Request is valid",
"job": {
"Id": 1,
"Jobtitle": "Test Job",
"Description": "Asthma",
"Medicalcondition": "Asthma",
"Language": "English",
"Racereligion": "Chinese",
"Agegender": "50, Female",
"Hourlyprice": 42,
"Type": "caregiver",
"Date": "2019-05-12T00:00:00+08:00",
"Starttime": "2018-07-20T12:15:00+08:00",
"Endtime": "2018-07-20T18:15:00+08:00",
"Address": "1 Cluny Road",
"Latitude": 1.3152057,
"Longitude": 103.8162553,
"Creator": "xiurobert"
}
}
and minified (the one that I'm attempting to construct a JSONObject from)并缩小(我试图从中构造 JSONObject 的那个)
String json = "{\"status\":200,\"message\":\"200 Request is valid\",\"job\":{\"Id\":1,\"Jobtitle\":\"Test Job\",\"Description\":\"Asthma\",\"Medicalcondition\":\"Asthma\",\"Language\":\"English\",\"Racereligion\":\"Chinese\",\"Agegender\":\"50, Female\",\"Hourlyprice\":42,\"Type\":\"caregiver\",\"Date\":\"2019-05-12T00:00:00+08:00\",\"Starttime\":\"2018-07-20T12:15:00+08:00\",\"Endtime\":\"2018-07-20T18:15:00+08:00\",\"Address\":\"1 Cluny Road\",\"Latitude\":1.3152057,\"Longitude\":103.8162553,\"Creator\":\"xiurobert\"}}";
Constructor构造函数
JSONObject jsonObject = new JSONObject(json)
However, this resulted in a java.lang.RuntimeException: Cannot evaluate org.json.JSONObject.toString()
但是,这导致了java.lang.RuntimeException: Cannot evaluate org.json.JSONObject.toString()
I've even tried using gson to parse the string first and then output it back to the new JSONObject but it seems like it didn't work.我什至尝试过使用 gson 先解析字符串,然后将 output 解析回新的 JSONObject,但它似乎不起作用。
Any reasons why?有什么理由吗?
This is working perfectly 这很正常
String json = "{\"status\":200,\"message\":\"200 Request is valid\",\"job\":{\"Id\":1,\"Jobtitle\":\"Test Job\",\"Description\":\"Asthma\",\"Medicalcondition\":\"Asthma\",\"Language\":\"English\",\"Racereligion\":\"Chinese\",\"Agegender\":\"50, Female\",\"Hourlyprice\":42,\"Type\":\"caregiver\",\"Date\":\"2019-05-12T00:00:00+08:00\",\"Starttime\":\"2018-07-20T12:15:00+08:00\",\"Endtime\":\"2018-07-20T18:15:00+08:00\",\"Address\":\"1 Cluny Road\",\"Latitude\":1.3152057,\"Longitude\":103.8162553,\"Creator\":\"xiurobert\"}}";
try
{
JSONObject jsonObject = new JSONObject(json);
Log.i("TAG", "a: "+jsonObject.toString());
} catch (JSONException e)
{
e.printStackTrace();
}
where are you getting error 你在哪里出错
In my case i have used org.json.JSONObject
and org.json.JSONArray
.就我而言,我使用org.json.JSONObject
和org.json.JSONArray
。 Then i changed the imports to org.json.simple.JSONObject
and org.json.simple.JSONArray
and my code worked without any issues.然后我将导入更改为org.json.simple.JSONObject
和org.json.simple.JSONArray
并且我的代码没有任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.