JSON 解析错误。密钥存在但未找到
JSON parsing error. Key not found even though it exists
这是我的代码片段。我正在尝试从 PUBNUB 向我的系统发送 JSON 消息并解析它。我的密钥是"CMD"。
这是我从 pubnub {"CMD":"hey"}
发送的内容
public void successCallback(String channel, Object message) {
Object PlainTextMessage = new AESDecrypt().Decrypt(message);
JSONObject jsonMsg = new JSONObject(PlainTextMessage);
try {
String command = jsonMsg.getString("CMD");
System.out.println(command);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是我得到的错误
org.json.JSONException: JSONObject["CMD"] not found. at
org.json.JSONObject.get(JSONObject.java:405) at
org.json.JSONObject.getString(JSONObject.java:586) at
PubNub.Subscribe.successCallback(Subscribe.java:45) at
com.pubnub.api.Callback.successWrapperCallback(Unknown Source) at
com.pubnub.api.PubnubCore.invokeSubscribeCallback(Unknown Source) at
com.pubnub.api.PubnubCore.access00(Unknown Source) at
com.pubnub.api.PubnubCore.handleResponse(Unknown Source) at
com.pubnub.api.SubscribeWorker.process(Unknown Source) at
com.pubnub.api.Worker.run(Unknown Source) at
java.lang.Thread.run(Thread.java:745)
我不知道我做错了什么。我尝试转义双引号,但没有用。
谁能告诉我我做错了什么?
提前致谢
请参阅 .org.json
的文档
http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject(java.lang.String)
JSONObject 的构造函数采用字符串而不是对象,因此需要进行强制转换
JSONObject
public JSONObject(java.lang.String source)
throws JSONException
Construct a JSONObject from a source JSON text string. This is the
most commonly used JSONObject constructor.
参数:
source - A string beginning with { (left brace) and ending with }
(right brace). Throws:
JSONException - If there is a syntax error in the source string or a
duplicated key.
这是我的代码片段。我正在尝试从 PUBNUB 向我的系统发送 JSON 消息并解析它。我的密钥是"CMD"。
这是我从 pubnub {"CMD":"hey"}
public void successCallback(String channel, Object message) {
Object PlainTextMessage = new AESDecrypt().Decrypt(message);
JSONObject jsonMsg = new JSONObject(PlainTextMessage);
try {
String command = jsonMsg.getString("CMD");
System.out.println(command);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是我得到的错误
org.json.JSONException: JSONObject["CMD"] not found. at org.json.JSONObject.get(JSONObject.java:405) at org.json.JSONObject.getString(JSONObject.java:586) at PubNub.Subscribe.successCallback(Subscribe.java:45) at com.pubnub.api.Callback.successWrapperCallback(Unknown Source) at com.pubnub.api.PubnubCore.invokeSubscribeCallback(Unknown Source) at com.pubnub.api.PubnubCore.access00(Unknown Source) at com.pubnub.api.PubnubCore.handleResponse(Unknown Source) at com.pubnub.api.SubscribeWorker.process(Unknown Source) at com.pubnub.api.Worker.run(Unknown Source) at java.lang.Thread.run(Thread.java:745)
我不知道我做错了什么。我尝试转义双引号,但没有用。
谁能告诉我我做错了什么?
提前致谢
请参阅 .org.json
http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject(java.lang.String)
JSONObject 的构造函数采用字符串而不是对象,因此需要进行强制转换
JSONObject
public JSONObject(java.lang.String source)
throws JSONException
Construct a JSONObject from a source JSON text string. This is the most commonly used JSONObject constructor.
参数:
source - A string beginning with { (left brace) and ending with } (right brace). Throws:
JSONException - If there is a syntax error in the source string or a duplicated key.