2015-06-23 126 views
1

我用来解析JSON的类不允许在任何字符串中包含冒号。例如,这会导致解析错误:解析包含冒号的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 = " "; 
} 
+0

您是否试图用反斜杠逃避冒号? – MiiinimalLogic

+0

这看起来像反编译或自动生成的代码。如果它是自动生成的,您是否拥有从中生成的源?这可能更容易修改。 –

+0

@MininimalLogic它是一个字符串,应该没有理由必须在其中冒号冒号。问题是如何解决解析代码,而不是如何解决它。 –

回答

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 

希望能有所帮助。

+0

我使用的类应该与您提供的类相同。由于它被反编译,我的可能有问题。现在它工作得很好。谢谢! – Matt

相关问题