【问题标题】:org.json.JSONObject creating JSONObject from string threw java.lang.RuntimeExceptionorg.json.JSONObject 从字符串创建 JSONObject 抛出 java.lang.RuntimeException
【发布时间】:2018-07-21 02:00:41
【问题描述】:

我一直在试图弄清楚为什么从字符串 json 构造 org.json.JSONObject 不会工作很长时间。

这里是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"
    }
}

并缩小(我试图从中构造 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\"}}";

构造函数

JSONObject jsonObject = new JSONObject(json)

但是,这导致java.lang.RuntimeException: Cannot evaluate org.json.JSONObject.toString()

我什至尝试过先使用 gson 解析字符串,然后将其输出回新的 JSONObject,但似乎不起作用。

有什么原因吗?

【问题讨论】:

标签: java android json org.json


【解决方案1】:

这是完美的工作

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

你哪里出错了

【讨论】:

  • 每当我尝试创建字符串时都会抛出 JSONException
  • 上面的代码工作正常,所以你想从那个jsonObject创建一个字符串?
  • 如果您想从该 jsonObject 创建一个字符串,只需简单地调用 jsonObject.toString(),或者如果您想要其他详细信息。
  • 对不起,我的错,我需要使用 org.json 库,我正在尝试在一个单元休息中为 android 做它
猜你喜欢
  • 1970-01-01
  • 2016-04-07
  • 2016-12-24
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 2022-01-02
相关资源
最近更新 更多