为什么 eval() 不能在这里反序列化这样一个简单的 JSON 对象?

发布于 2024-09-06 04:28:52 字数 391 浏览 5 评论 0原文

我正在尝试使用 eval() 函数通过 eval 函数反序列化此 JSON 文本。

var personJSON = {
  "FirstName": "Burak",
  "LastName": "Ozdogan",
  "Id": "001",
  "Department": "Information Technologies"
};

var personBurakOzdogan = eval('(' + personJSON + ')');

但我收到此错误:

*Microsoft JScript compilation error: Expected ']'*

是否有一些我无法捕获的跳过的内容?

谢谢

I am trying to use eval() function to deserialize this JSON text by using eval function.

var personJSON = {
  "FirstName": "Burak",
  "LastName": "Ozdogan",
  "Id": "001",
  "Department": "Information Technologies"
};

var personBurakOzdogan = eval('(' + personJSON + ')');

But I am getting this error:

*Microsoft JScript compilation error: Expected ']'*

Is there something that I skip which I cannot catch?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦过后 2024-09-13 04:28:52

你拥有的不是 JSON 文本。它已经是一个 JSON 对象。所以你根本不需要使用eval。您可以直接访问和操作其属性:

alert(personJSON.FirstName);

What you have is not JSON text. It is already a JSON object. So you don't need to use eval at all. You can directly access and manipulate its properties:

alert(personJSON.FirstName);
无妨# 2024-09-13 04:28:52

尝试检查您的 personJSON 是否是包含真实 json 的包装器。例如,尝试编写:

var person = eval('(' + personJSON.Person + ')')

其中 Person 是服务序列化的类。

或者

尝试以下操作:

var person = eval('(' + personJSON.GetPersonResult + ')')

其中 GetPerson 是服务中的方法名称,加上 Result

Try to check if your personJSON is a wrapper that CONTAINS the real json. For example, try to write:

var person = eval('(' + personJSON.Person + ')')

where Person is the class serialized by the service.

OR

try this:

var person = eval('(' + personJSON.GetPersonResult + ')')

where GetPerson is the method name in the service, plus Result.

萤火眠眠 2024-09-13 04:28:52

你处理的不是一个字符串,而是一个 json 对象。您正在尝试将 json 对象评估为字符串以创建 json 对象。

var personJSON =
  '{"FirstName":"Burak","LastName":"Ozdogan","Id":"001","Department":"Information Technologies"}';

var personBurakOzdogan = eval('(' + personJSON + ')');

这应该有效,尽管没有多大意义。这更有意义:

var personBurakOzdogan = {
  "FirstName": "Burak",
  "LastName": "Ozdogan",
  "Id": "001",
  "Department": "Information Technologies"
};

you are not dealing with a string, but with a json object. You are trying to evaluate a json object as string to create a json object.

var personJSON =
  '{"FirstName":"Burak","LastName":"Ozdogan","Id":"001","Department":"Information Technologies"}';

var personBurakOzdogan = eval('(' + personJSON + ')');

this should work, although it doesn't make to much sense. this makes more sense:

var personBurakOzdogan = {
  "FirstName": "Burak",
  "LastName": "Ozdogan",
  "Id": "001",
  "Department": "Information Technologies"
};
瞄了个咪的 2024-09-13 04:28:52

您必须将字符串类型传递给变量,如下所示:

var personJSON = '{"FirstName":"Burak","LastName":"Ozdogan","Id":"001","Department":"Information Technologies"}';

You have to pass to the variable a string type as the code seen below:

var personJSON = '{"FirstName":"Burak","LastName":"Ozdogan","Id":"001","Department":"Information Technologies"}';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文