简体   繁体   中英

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.

Here is the json string (pretty)

{
    "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)

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()

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.

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 . Then i changed the imports to org.json.simple.JSONObject and org.json.simple.JSONArray and my code worked without any issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM