异常将对象转换为JSONObject

时间:2014-10-16 15:24:15

标签: java android json jsonobject

我正在尝试转换对象

Object jObj;
parser.parse(response);
jObj = parser.parse(response);

响应是

{"as":false,"autoToggleOff":false,"autoToggleSeconds":0,"cs":false,"deviceId":"e4e4217f-eef2-4cd8-9a87-21264ddff836","deviceName":"Virtual Switch One","deviceType":1,"displayOrder":0,"et":null,"he":false,"il":false,"isControllerBacked":true,"lastLevelUpdate":"\/Date(1413470244538-0400)\/","level":99,"levelPresets":[],"name":"Virtual Switch One","newLevel":null,"newPowerState":null,"newPowerTimerSeconds":null,"nextEventPowerLevel":null,"nextEventPowerOn":null,"nextEventTime":null,"nodeId":0,"pd":true,"providerDeviceId":"141009173344","pushUpdates":false,"roomId":null,"shortId":2,"sr":null,"st":null,"visible":true}

来自Object的JSONObject

JSONObject jsonObject;
jsonObject = (JSONObject) jObj;

我得到了

10-16 11:09:04.962: W/dalvikvm(14259): threadid=12: thread exiting with uncaught exception (group=0x41d21d40)

代码被try / catch

包围
} catch (JSONException e) {
    e.printStackTrace();    
} catch (ParseException e){
    e.printStackTrace();
}

我应该尝试抓住一个例外吗?几小时前代码工作正常,所以我真的很难知道发生了什么变化......

谢谢!

2 个答案:

答案 0 :(得分:0)

  

java.lang.ClassCastException:org.json.simple.JSONObject不能   强制转换为org.json.JSONObject“我会调查那个......

好像你的导入错了。 parser.parse似乎返回另一种jsonobject

答案 1 :(得分:0)

原来我正在使用

import org.json.JSONObject;

而不是

import org.json.simple.JSONObject