简体   繁体   English

带有JSON.parse的字符串中的无效字符

[英]Invalid Character in String with JSON.parse

Can someone help me determine what the issue is with this string... 有人可以帮我确定此字符串的问题是什么...

Visual Studio Escaped String Visual Studio转义字符串

"({ID:[\"1\"],ElementID:[\"1\"],UserHashKey:[\"39f46b38-e4c5-4081-aba4-28158d271952\"],[new String(\"FirstName88\")]:[new String(\"Ryan\")],[new String(\"LastName89\")]:[new String(\"Leesh\")],[new String(\"Type90\")]:[new String(\"Dental\")],[new String(\"Test[\\\"\\\"]91\")]:[new String(\"Help Me\")],UserCreated:[\"rleesh@hotmail.com\"],Created:[\"9/23/2015\"],Updated:[\"9/23/2015\"]})"

Visual Studio Text Visualizer Visual Studio文字可视化器

({ID:["1"],ElementID:["1"],UserHashKey:["39f46b38-e4c5-4081-aba4-28158d271952"],[new String("FirstName88")]:[new String("Ryan")],[new String("LastName89")]:[new String("Leesh")],[new String("Type90")]:[new String("Dental")],[new String("Test[\"\"]91")]:[new String("Help Me")],UserCreated:["ryanleesh@hotmail.com"],Created:["9/23/2015"],Updated:["9/23/2015"]})

I receive the same error for both eval(str) and JSON.parse(str) 我同时收到eval(str)和JSON.parse(str)的相同错误

Invalid character 无效字符

I know the issue comes from the following [\\"\\"], but I can't figure out how to manipulate it to pass the parsing engine. 我知道问题来自以下[\\“ \\”],但我不知道如何处理它以传递解析引擎。

Code is as follows: 代码如下:

String.prototype.toFieldDef = function () {
    var str = '({' + this + '})';
str = str.replace(/(?:\r\n|\r|\n)/g, '<br />');

try {
    //str = JSON && JSON.parse(str) || $.parseJSON(str);
    str = eval(str);
} catch (e) {
    if (e instanceof SyntaxError) {
        try{
            str = eval('"'+str+'"')
        } catch (f) {
            if (f instanceof SyntaxError) {
                alert(f.message);
            }
        } 
    }
}

return str;

}; };

The main problem that I see is that some of the object keys are specified to be arrays. 我看到的主要问题是某些对象键被指定为数组。 Consider: 考虑:

js> x="{[new String(\"FirstName88\")]:[new String(\"Ryan\")]}";
js> eval(x)
typein:2: SyntaxError: invalid label:
typein:2: {[new String("FirstName88")]:[new String("Ryan")]}
typein:2: ...........................^

Replacing the square brackets with parentheses doesn't help, at least when using JavaScript-C or v8. 至少在使用JavaScript-C或v8时,用括号替换方括号没有帮助。 You'll either have to use a string literal, or programmatically construct (or reconstruct) the string to be evaluated. 您要么必须使用字符串文字,要么以编程方式构造(或重建)要评估的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM