如何在可能是简单字符串或字符串对象的字符串对象上安全地使用JSON.parse?

时间:2023-01-06 19:21:19

I need to be able to parse a string in an object format or in a plain string format. what is the safest way to do this?

我需要能够以对象格式或普通字符串格式解析字符串。最安全的方法是什么?

I tried JSON.parse(data) but it does not work in case that data is a plain string.

我尝试过JSON.parse(数据)但是在数据是普通字符串的情况下它不起作用。


EDIT - The chosen solution

Thanks to you, this is how I solved the problem:

谢谢你,这就是我解决问题的方法:

try {
    dataObj = JSON.parse(data);
} catch (err) {
    if (typeof data === "object") {
        dataObj = data;
    } else {
        dataObj = {};
    }
}

3 个解决方案

#1


3  

Use try catch:

使用try catch:

var result;
try {
   result = JSON.parse(data);
} catch (err) {
   if (typeof data == 'string') result = data;
   else console.error(err);
}

#2


2  

Create yourself a helper function and use that.

创建一个帮助函数并使用它。

function parseValue(value) {
    try
    {
        return JSON.parse(value);
    }
    catch (ex)
    {
        // JSON format was invalid
        // handle errors is you need to
    }

    return value;
}

If you're brave enough you can also extend String.prototype so calling it would become really straight forward.

如果你足够勇敢,你也可以扩展String.prototype,因此调用它将变得非常简单。

String.prototype.parseJSON = String.prototype.parseJSON || function() {
    try
    {
        return JSON.parse(this);
    }
    catch (ex)
    {
        // JSON format was invalid
        // handle errors is you need to
    }

    return this;
};

And then you'd simply call it this way:

然后你只需这样称呼它:

// string in a variable
var s = "Some non-JSON string";
s.parseJSON();
// string literal
'{"b":true,"n":1}'.parseJSON();

#3


0  

Assuming the JSON will always have the same format, you could also check whether the string starts with a {" (or just {), and only then parse it with JSON.parse.

假设JSON将始终具有相同的格式,您还可以检查字符串是以{“(或只是{)开头,然后仅使用JSON.parse进行解析。

It really depends of the possible input though.

这实际上取决于可能的输入。

#1


3  

Use try catch:

使用try catch:

var result;
try {
   result = JSON.parse(data);
} catch (err) {
   if (typeof data == 'string') result = data;
   else console.error(err);
}

#2


2  

Create yourself a helper function and use that.

创建一个帮助函数并使用它。

function parseValue(value) {
    try
    {
        return JSON.parse(value);
    }
    catch (ex)
    {
        // JSON format was invalid
        // handle errors is you need to
    }

    return value;
}

If you're brave enough you can also extend String.prototype so calling it would become really straight forward.

如果你足够勇敢,你也可以扩展String.prototype,因此调用它将变得非常简单。

String.prototype.parseJSON = String.prototype.parseJSON || function() {
    try
    {
        return JSON.parse(this);
    }
    catch (ex)
    {
        // JSON format was invalid
        // handle errors is you need to
    }

    return this;
};

And then you'd simply call it this way:

然后你只需这样称呼它:

// string in a variable
var s = "Some non-JSON string";
s.parseJSON();
// string literal
'{"b":true,"n":1}'.parseJSON();

#3


0  

Assuming the JSON will always have the same format, you could also check whether the string starts with a {" (or just {), and only then parse it with JSON.parse.

假设JSON将始终具有相同的格式,您还可以检查字符串是以{“(或只是{)开头,然后仅使用JSON.parse进行解析。

It really depends of the possible input though.

这实际上取决于可能的输入。