解析包含冒号的 JSON 字符串
Posted
技术标签:
【中文标题】解析包含冒号的 JSON 字符串【英文标题】:Parse JSON string containing a colon 【发布时间】:2015-09-09 18:03:54 【问题描述】:我用来解析 JSON 的类不允许您在任何字符串中使用冒号。例如,这会导致解析错误:
"Test:": "Just a test"
关于如何使此类忽略字符串中的冒号的任何想法?这是我正在使用的课程:
class JSON
static var inst;
var text;
function JSON()
static function getInstance()
if (inst == null)
inst = new cinqetdemi.JSON();
return (inst);
static function stringify(arg)
var _local3;
var _local2;
var _local6;
var _local1 = "";
var _local4;
switch (typeof (arg))
case "object" :
if (arg)
if (arg instanceof Array)
_local2 = 0;
while (_local2 < arg.length)
_local4 = stringify(arg[_local2]);
if (_local1)
_local1 = _local1 + ",";
_local1 = _local1 + _local4;
_local2++;
return (("[" + _local1) + "]");
else if (typeof (arg.toString) != "undefined")
for (_local2 in arg)
_local4 = arg[_local2];
if ((typeof (_local4) != "undefined") && (typeof (_local4) != "function"))
_local4 = stringify(_local4);
if (_local1)
_local1 = _local1 + ",";
_local1 = _local1 + ((stringify(_local2) + ":") + _local4);
return (("" + _local1) + "");
return ("null");
case "number" :
return ((isFinite(arg) ? (String(arg)) : "null"));
case "string" :
_local6 = arg.length;
_local1 = "\"";
_local2 = 0;
while (_local2 < _local6)
_local3 = arg.charAt(_local2);
if (_local3 >= " ")
if ((_local3 == "\\") || (_local3 == "\""))
_local1 = _local1 + "\\";
_local1 = _local1 + _local3;
else
switch (_local3)
case "\b" :
_local1 = _local1 + "\\b";
break;
case "\f" :
_local1 = _local1 + "\\f";
break;
case newline :
_local1 = _local1 + "\\n";
break;
case "\r" :
_local1 = _local1 + "\\r";
break;
case "\t" :
_local1 = _local1 + "\\t";
break;
default :
_local3 = _local3.charCodeAt();
_local1 = _local1 + (("\\u00" + Math.floor(_local3 / 16).toString(16)) + (_local3 % 16).toString(16));
_local2 = _local2 + 1;
return (_local1 + "\"");
case "boolean" :
return (String(arg));
return ("null");
static function parse(text)
if (!text.length)
throw new Error("JSONError: Text missing");
var _local1 = getInstance();
_local1.at = 0;
_local1.ch = " ";
_local1.text = text;
return (_local1.value());
function error(m)
var _local2 = ((("JSONError: " + m) + " at ") + (at - 1)) + newline;
_local2 = _local2 + (text.substr(at - 10, 20) + newline);
_local2 = _local2 + " ^";
throw new Error(_local2);
function next()
ch = text.charAt(at);
at = at + 1;
return (ch);
function white()
while (ch)
if (ch <= " ")
next();
else if (ch == "/")
switch (next())
case "/" :
while ((next() && (ch != newline)) && (ch != "\r"))
break;
case "*" :
next();
while (true)
if (ch)
if (ch == "*")
if (next() == "/")
next();
break;
else
next();
else
error("Unterminated comment");
break;
default :
error("Syntax error");
else
break;
function str()
var _local5;
var _local2 = "";
var _local4;
var _local3;
var _local6 = false;
if ((ch == "\"") || (ch == "'"))
var _local7 = ch;
while (next())
if (ch == _local7)
next();
return (_local2);
else if (ch == "\\")
switch (next())
case "b" :
_local2 = _local2 + "\b";
break;
case "f" :
_local2 = _local2 + "\f";
break;
case "n" :
_local2 = _local2 + newline;
break;
case "r" :
_local2 = _local2 + "\r";
break;
case "t" :
_local2 = _local2 + "\t";
break;
case "u" :
_local3 = 0;
_local5 = 0;
while (_local5 < 4)
_local4 = parseInt(next(), 16);
if (!isFinite(_local4))
_local6 = true;
break;
_local3 = (_local3 * 16) + _local4;
_local5 = _local5 + 1;
if (_local6)
_local6 = false;
break;
_local2 = _local2 + String.fromCharCode(_local3);
break;
default :
_local2 = _local2 + ch;
else
_local2 = _local2 + ch;
error("Bad string");
function key()
var _local2 = ch;
var _local6 = false;
var _local3 = text.indexOf(":", at);
var _local4 = text.indexOf("\"", at);
var _local5 = text.indexOf("'", at);
if (((_local4 <= _local3) && (_local4 > -1)) || ((_local5 <= _local3) && (_local5 > -1)))
_local2 = str();
white();
if (ch == ":")
return (_local2);
else
error("Bad key");
while (next())
if (ch == ":")
return (_local2);
if (ch <= " ")
else
_local2 = _local2 + ch;
error("Bad key");
function arr()
var _local2 = [];
if (ch == "[")
next();
white();
if (ch == "]")
next();
return (_local2);
while (ch)
if (ch == "]")
next();
return (_local2);
_local2.push(value());
white();
if (ch == "]")
next();
return (_local2);
else if (ch != ",")
break;
next();
white();
error("Bad array");
function obj()
var _local3;
var _local2 = ;
if (ch == "")
next();
white();
if (ch == "")
next();
return (_local2);
while (ch)
if (ch == "")
next();
return (_local2);
_local3 = this.key();
if (ch != ":")
break;
next();
_local2[_local3] = value();
white();
if (ch == "")
next();
return (_local2);
else if (ch != ",")
break;
next();
white();
error("Bad object");
function num()
var _local2 = "";
var _local3;
if (ch == "-")
_local2 = "-";
next();
while (((((ch >= "0") && (ch <= "9")) || (ch == "x")) || ((ch >= "a") && (ch <= "f"))) || ((ch >= "A") && (ch <= "F")))
_local2 = _local2 + ch;
next();
if (ch == ".")
_local2 = _local2 + ".";
next();
while ((ch >= "0") && (ch <= "9"))
_local2 = _local2 + ch;
next();
if ((ch == "e") || (ch == "E"))
_local2 = _local2 + ch;
next();
if ((ch == "-") || (ch == "+"))
_local2 = _local2 + ch;
next();
while ((ch >= "0") && (ch <= "9"))
_local2 = _local2 + ch;
next();
_local3 = Number(_local2);
if (!isFinite(_local3))
error("Bad number");
return (_local3);
function word()
switch (ch)
case "t" :
if (((next() == "r") && (next() == "u")) && (next() == "e"))
next();
return (true);
break;
case "f" :
if ((((next() == "a") && (next() == "l")) && (next() == "s")) && (next() == "e"))
next();
return (false);
break;
case "n" :
if (((next() == "u") && (next() == "l")) && (next() == "l"))
next();
return (null);
break;
error("Syntax error");
function value()
white();
switch (ch)
case "" :
return (obj());
case "[" :
return (arr());
case "\"" :
case "'" :
return (str());
case "-" :
return (num());
return ((((ch >= "0") && (ch <= "9")) ? (num()) : (word())));
var at = 0;
var ch = " ";
【问题讨论】:
您是否尝试用反斜杠转义冒号? 这看起来像是反编译或自动生成的代码。如果它是自动生成的,你有它生成的来源吗?这可能更容易修改。 我会选择不同的解析器。例如,JSON 不允许字符串使用单引号,这个解析器允许。 JSON 不允许非转义的控制字符,这个解析器允许。 看到这不仅拒绝某些字符串中的冒号,还接受由单引号分隔的字符串,这不是一个 JSON 解析器。它不适用于您的情况的原因是key()
函数编写得非常糟糕。它有很多问题,我不确定是否值得花时间尝试调试它,谁知道代码的其他部分中是否还有其他你尚未发现的错误。
它解析 cmets。那是大错特错了。它不会在基本页面之外解析 Unicode,因此没有适合您的表情符号 :-)
【参考方案1】:
看看my answer of this question 并使用我在那里提到的JSON
类,它与您的测试内容工作正常:
import JSON;
var json = new JSON();
var data:String = '"Test:": "Just a test"';
trace(json.parse(data)['Test:']); // gives : Just a test
希望能有所帮助。
【讨论】:
我使用的课程应该与您提供的课程相同。我的可能有问题,因为它被反编译了。现在工作得很好。谢谢! 如何在 java-script 中做到这一点 @sasharomanov 在 JS 中几乎是一样的:console.log(JSON.parse('"Test:": "Just a test"').Test); // gives : Just a test
...
@akmozo,我的意思是如何在不从外部源导入 JSON 的情况下使用它,如何使用纯 JSON.parse('"Test:":"Just a test"');
而不会出现任何错误
@sasharomanov 如果您说的是 javascript,那么在使用 JSON
时应该不会出现任何错误,因为所有现代浏览器都支持原生 JSON
编码和解码。 ActionScript 3 还具有原生的 JSON
功能(请参阅 here),但如果您谈论的是 ActionScript 2,那么您应该从外部源导入 JSON。以上是关于解析包含冒号的 JSON 字符串的主要内容,如果未能解决你的问题,请参考以下文章
java后台怎么解析含有特殊字符的json字符串,如含有逗号,冒号等特殊字符
SpringBoot Fastjson解析多层嵌套复杂Json字符串