简体   繁体   English

org.json.JSONObject 从字符串创建 JSONObject 抛出 java.lang.RuntimeException

[英]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.JSONObjectorg.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.JSONObjectorg.json.simple.JSONArray并且我的代码没有任何问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找到所需的java.lang.String org.json.JSONObject - required java.lang.String found org.json.JSONObject java.lang.AssertionError:预期:org.json.JSONObject &lt;{“ feedback”:[]}&gt;但是:org.json.JSONObject &lt;{“ feedback”:[]}&gt; - java.lang.AssertionError: expected: org.json.JSONObject<{“feedback”:[]}> but was: org.json.JSONObject<{“feedback”:[]}> java.lang.ClassCastException: java.lang.String 不能转换为 org.json.JSONObject - java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject java.lang.ClassNotFoundException: org.json.JSONObject - java.lang.ClassNotFoundException: org.json.JSONObject Java:org.json.JSONObject-创建新实例可从大十进制中删除尾随零 - Java: org.json.JSONObject - creating a new instance removes trailing zeros from big decimals 我的Java Restful Web服务中的java.lang.ClassNotFoundException:org.json.JSONObject - java.lang.ClassNotFoundException: org.json.JSONObject in my java restful webservice 在JAVA(org.json)中从字符串创建JSONObject - Creating JSONObject from string in JAVA (org.json) java.lang.ClassCastException:org.json.JSONObject $ 1无法转换为Task(我的自定义ParseObject) - java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to Task (my custom ParseObject) java.lang.ClassCastException:MapReduce程序中的org.json.JSONObject类 - java.lang.ClassCastException: class org.json.JSONObject in MapReduce program 无法解析方法&#39;fromJson(org.json.JSONObject,java.lang.Class <me.myrottentomatoes.Result[]> )&#39; - Cannot resolve method 'fromJson(org.json.JSONObject, java.lang.Class<me.myrottentomatoes.Result[]>)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM