最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why eval() isn't working here to deserialize such a simple JSON object? - Stack Overflow

programmeradmin5浏览0评论

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 pilation error: Expected ']'*

Is there something that I skip which I cannot catch?

Thanks

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 pilation error: Expected ']'*

Is there something that I skip which I cannot catch?

Thanks

Share Improve this question edited Jan 7, 2020 at 13:24 Mickael Lherminez 6951 gold badge11 silver badges30 bronze badges asked Jun 20, 2010 at 12:31 pencilCakepencilCake 53.4k87 gold badges236 silver badges377 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

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);

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.

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"
};

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"}';
发布评论

评论列表(0)

  1. 暂无评论