我的json看起来像这样
{"FROM_JID":"ff2fbe2f1e856a0c5c93616a463f7bdeef9bf7a0","TO_JID":"30df65445882e2c83783aad963ae64ca9ab6891a","TYPE_ID":"1","PLATFORM":"IOS","CONTENT":"{\"FROM_JID\":\"ff2fbe2f1e856a0c5c93616a463f7bdeef9bf7a0\",\"FROM_HOST\":\"ssdevim.mtouche-mobile.com\",\"FROM_JNAME\":\"G0173A2D\",\"TO_JID\":\"30df65445882e2c83783aad963ae64ca9ab6891a\",\"TO_HOST\":\"ssdevim.mtouche-mobile.com\",\"MESSAGE_ID\":\"074f36be91093e9c1490a9d9c984e15d\",\"MESSAGE_TYPE\":\"not_avt\",\"STAMP\":\"2016-01-13T10:21:03.761817Z\",\"MESSAGE\":{\"mt\":\"not_avt\",\"jid\":\"ff2fbe2f1e856a0c5c93616a463f7bdeef9bf7a0@ssdevim.mtouche-mobile.com\"},\"BADGE\":14,\"CONTENT-AVAILABLE\":1,\"SOUND\":\"default\"}","DEVICE_ID":"54A89FDE-0981-46D2-895B-0445AFC4B072","PUSH_ID":"37257446ea74b2e9b688407aa8a6ad94dd270a9d450f37f2bc3f6c9af5362c69","CREATE_DATE":"2016-01-13","CREATE_TIME":"10:21:03"}
我的代码是
JSONParser content_parser = new JSONParser();
Object objMessage_Content = content_parser.parse(strContent);
JSONObject jsonObjectMessage_Content = (JSONObject) objMessage_Content;
String strMT=(String) jsonObjectMessage_Content.get("MESSAGE_TYPE");
String strM=(String) jsonObjectMessage_Content.get("MESSAGE");
我有一个例外
java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to java.lang.String
我查了json是有效的json.can,请帮助。
答案 0 :(得分:1)
我猜你正在使用gson
所以首先得到MESSAGE
- 这将是一个JSONObject
JsonObject message = jsonObjectMessage_Content.getAsJsonObject("MESSAGE");
然后你得到字符串MESSAGE_TYPE
message.getAsString ("MESSAGE_TYPE");
答案 1 :(得分:0)
您应该使用:
String strMT= jsonObjectMessage_Content.optString("MESSAGE_TYPE");
String strM= jsonObjectMessage_Content.optString("MESSAGE");