为什么 eval() 不能在这里反序列化这样一个简单的 JSON 对象?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你拥有的不是 JSON 文本。它已经是一个 JSON 对象。所以你根本不需要使用
eval
。您可以直接访问和操作其属性: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:尝试检查您的 personJSON 是否是包含真实 json 的包装器。例如,尝试编写:
其中
Person
是服务序列化的类。或者
尝试以下操作:
其中
GetPerson
是服务中的方法名称,加上Result
。Try to check if your personJSON is a wrapper that CONTAINS the real json. For example, try to write:
where
Person
is the class serialized by the service.OR
try this:
where
GetPerson
is the method name in the service, plusResult
.你处理的不是一个字符串,而是一个 json 对象。您正在尝试将 json 对象评估为字符串以创建 json 对象。
这应该有效,尽管没有多大意义。这更有意义:
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.
this should work, although it doesn't make to much sense. this makes more sense:
您必须将字符串类型传递给变量,如下所示:
You have to pass to the variable a string type as the code seen below: