views:

65

answers:

2

Hi,

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

+3  A: 

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);
Darin Dimitrov
Idiot me! Thanks Darin! :)I have enclosed that within ' ' and eval worked.
burak ozdogan
@burak, I think you're missing the point. Since you have it in object-literal form, there's no point in getting it in JSON...
J-P
@Darin: Actually this is why I am doing this practice. I had a problem to understand the exact difference between a JSON object and an onbect defined with Object-Literal feature of JavaScript. Thank you Darin once again.
burak ozdogan
+1  A: 

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