Actionscript:无效 JSON 解析似乎有效的输入
Actionscript: Invalid JSON parse input for what seems valid
我有以下JSON
{
"extras": {
"google.sent_time": 1502027522898,
"custom": "{\"a\":{\"message_data\":\"\",\"message_id\":\"749\",\"message_command\":\"MESSAGE\"},\"i\":\"899ec3dd\"}",
"from": "62572096498",
"alert": "Read More...",
"title": "New message",
"google.message_id": "0:2905559%2ecccafd7ecd"
}
}
使用
var jsonObj:Object = JSON.parse(str);
给出错误:
SyntaxError: Error #1132: Invalid JSON parse input.
at JSON$/parseCore()
at JSON$/parse()
我不明白这是为什么,JSON有效。
补充信息,
我尝试过并有效的解决方案如下,尽管之前和之后都是有效的。
var clean:String = str.split("\").join('');
clean = clean.replace('"custom":"{"a"', '"custom":{"a"');
clean = clean.replace('"}","from"', '"},"from"');
我相信 str
已经是一个 javascript 对象,所以没有什么可解析的,您可以像这样简单地分配它:
var jsonObj:Object = str;
但是我假设您需要解析并转换为对象您的 custom
属性:
a.extras.custom = JSON.parse("{\"a\":{\"message_data\":\"\",\"message_id\":\"749\",\"message_command\":\"MESSAGE\"},\"i\":\"899ec3dd\"}")
观察很少:
OP
中提供的 JSON 看起来像 JSON object
而不是 JSON string
。因此,无需解析整个对象。
- 因为
partialJsonObj.extras.custom
是一个 JSON 字符串解析它以转换成 JSON 对象。
演示
var partialJsonObj = {
"extras": {
"google.sent_time": 1502027522898,
"custom": "{\"a\":{\"message_data\":\"\",\"message_id\":\"749\",\"message_command\":\"MESSAGE\"},\"i\":\"899ec3dd\"}",
"from": "62572096498",
"alert": "Read More...",
"title": "New message",
"google.message_id": "0:2905559%2ecccafd7ecd"
}
};
partialJsonObj.extras.custom = JSON.parse(partialJsonObj.extras.custom);
var jsonObj:Object = partialJsonObj;
console.log(jsonObj);
如果此 "JSON" 是您的动作脚本的一部分,则它是 Object, not a JSON。
JSON.parse method won't work because accepts JSON-formatted String as first parameter and you pass and Object 代替。
如果您 load/import 来自 JSON 文件的脚本,JSON.parse 方法将起作用。
// importing the external JSON file
function loadJSON() {
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, decodeJSON);
loader.load(new URLRequest("test.json"));
}
// converting to actionscript Object
function decodeJSON(e:Event):void {
var loader:URLLoader = URLLoader(e.target) ;
var jsonObj:Object = JSON.parse(loader.data);
trace(jsonObj["extras"]["custom"]["a"]["message_id"]);
}
loadJSON();
如果要访问 "custom" 值,请取消注释 JSON 文件中的双引号:
"custom": {"a":{"message_data":"","message_id":"749","message_command":"MESSAGE"},"i":"899ec3dd"},
我有以下JSON
{
"extras": {
"google.sent_time": 1502027522898,
"custom": "{\"a\":{\"message_data\":\"\",\"message_id\":\"749\",\"message_command\":\"MESSAGE\"},\"i\":\"899ec3dd\"}",
"from": "62572096498",
"alert": "Read More...",
"title": "New message",
"google.message_id": "0:2905559%2ecccafd7ecd"
}
}
使用
var jsonObj:Object = JSON.parse(str);
给出错误:
SyntaxError: Error #1132: Invalid JSON parse input.
at JSON$/parseCore()
at JSON$/parse()
我不明白这是为什么,JSON有效。
补充信息,
我尝试过并有效的解决方案如下,尽管之前和之后都是有效的。
var clean:String = str.split("\").join('');
clean = clean.replace('"custom":"{"a"', '"custom":{"a"');
clean = clean.replace('"}","from"', '"},"from"');
我相信 str
已经是一个 javascript 对象,所以没有什么可解析的,您可以像这样简单地分配它:
var jsonObj:Object = str;
但是我假设您需要解析并转换为对象您的 custom
属性:
a.extras.custom = JSON.parse("{\"a\":{\"message_data\":\"\",\"message_id\":\"749\",\"message_command\":\"MESSAGE\"},\"i\":\"899ec3dd\"}")
观察很少:
OP
中提供的 JSON 看起来像JSON object
而不是JSON string
。因此,无需解析整个对象。- 因为
partialJsonObj.extras.custom
是一个 JSON 字符串解析它以转换成 JSON 对象。
演示
var partialJsonObj = {
"extras": {
"google.sent_time": 1502027522898,
"custom": "{\"a\":{\"message_data\":\"\",\"message_id\":\"749\",\"message_command\":\"MESSAGE\"},\"i\":\"899ec3dd\"}",
"from": "62572096498",
"alert": "Read More...",
"title": "New message",
"google.message_id": "0:2905559%2ecccafd7ecd"
}
};
partialJsonObj.extras.custom = JSON.parse(partialJsonObj.extras.custom);
var jsonObj:Object = partialJsonObj;
console.log(jsonObj);
如果此 "JSON" 是您的动作脚本的一部分,则它是 Object, not a JSON。
JSON.parse method won't work because accepts JSON-formatted String as first parameter and you pass and Object 代替。
如果您 load/import 来自 JSON 文件的脚本,JSON.parse 方法将起作用。
// importing the external JSON file
function loadJSON() {
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, decodeJSON);
loader.load(new URLRequest("test.json"));
}
// converting to actionscript Object
function decodeJSON(e:Event):void {
var loader:URLLoader = URLLoader(e.target) ;
var jsonObj:Object = JSON.parse(loader.data);
trace(jsonObj["extras"]["custom"]["a"]["message_id"]);
}
loadJSON();
如果要访问 "custom" 值,请取消注释 JSON 文件中的双引号:
"custom": {"a":{"message_data":"","message_id":"749","message_command":"MESSAGE"},"i":"899ec3dd"},