解析包含冒号的JSON字符串
我用来解析JSON的类不允许在任何字符串中使用冒号。例如,这将导致解析时出错:解析包含冒号的JSON字符串,json,actionscript-2,Json,Actionscript 2,我用来解析JSON的类不允许在任何字符串中使用冒号。例如,这将导致解析时出错: { "Test:": "Just a test" } 关于如何使这个类忽略字符串中的冒号,有什么想法吗?以下是我正在使用的类: class JSON { static var inst; var text; function JSON() { } static function getInstance() { if (inst == null) {
{
"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 = " ";
}
classjson{
静态无功仪;
var文本;
函数JSON(){
}
静态函数getInstance(){
如果(inst==null){
inst=new-cinqetdemi.JSON();
}
返回(inst);
}
静态函数stringify(arg){
var_local3;
var_local2;
var_local6;
var_local1=“”;
var_local4;
开关(类型(arg)){
案例“对象”:
如果(arg){
if(arg instanceof数组){
_local2=0;
while(_local2=“”){
如果(_local3==“\\”)|(_local3==“\”)){
_local1=\u local1+“\\”;
}
_local1=\u local1+\u local3;
}否则{
开关(_local3){
案例“\b”:
_local1=\u local1+“\\b”;
打破
案例“\f”:
_local1=\u local1+“\\f”;
打破
案例换行:
_local1=\u local1+“\\n”;
打破
案例“\r”:
_local1=\u local1+“\\r”;
打破
案例“\t”:
_local1=\u local1+“\\t”;
打破
违约:
_local3=_local3.charCodeAt();
_local1=_local1+(“\\u00”+数学地板(_local3/16).toString(16))+(_local3%16).toString(16));
}
}
_local2=_local2+1;
}
返回(\u local1+“\”);
案例“布尔”:
返回(字符串(arg));
}
返回(“空”);
}
静态函数解析(文本){
如果(!text.length){
抛出新错误(“JSONError:Text missing”);
}
var_local1=getInstance();
_local1.at=0;
_local1.ch=“”;
_local1.text=文本;
返回(_local1.value());
}
函数错误(m){
var_local2=((((((((((((((((“JSONError:“+m”)+“at”))+(at-1))+换行符;
_local2=_local2+(text.substr(at-10,20)+换行符);
_local2=_local2+“^”;
抛出新错误(_local2);
}
函数next(){
ch=text.charAt(at);
at=at+1;
返回(ch);
}
函数white(){
while(ch){
如果(ch=“0”)&&(ch查看并使用我在这里提到的JSON
类,它可以很好地处理您的测试内容:
import JSON;
var json = new JSON();
var data:String = '{"Test:": "Just a test"}';
trace(json.parse(data)['Test:']); // gives : Just a test
希望这能有所帮助。您是否尝试用反斜杠来转义冒号?这看起来像是反编译或自动生成的代码。如果是自动生成的,您有生成它的源代码吗?这可能更容易修改。我会选择其他解析器。例如,JSON不允许字符串使用单引号,这个解析器允许。JSON不允许使用未转义的控制字符,这个解析器允许。考虑到这不仅拒绝了某些字符串中的冒号,而且还接受了由单引号分隔的字符串,这不是一个JSON解析器。它不适合您的情况的原因是键()
函数编写得非常糟糕。它有很多问题,我不确定是否值得花时间调试它,因为谁知道在代码的其他部分中是否还有您尚未发现的其他错误。它解析注释。这是非常错误的。它不会在基页之外解析Unicode,因此没有表情符号:-)我使用的类应该与您提供的类相同。我的类在反编译时可能有问题。它现在工作得很好。谢谢!如何在java中做到这一点-script@sasharomanov在JS:console.log(JSON.parse('{“Test::“Just a Test”}').Test)中几乎是一样的;//给出:Just a Test
。。