2

I have an invalid Json string that I need to parse to an object and feed to a viewmodel:

var s = "{ a: new Date(1400000000000) }"; // from a server, can't change this
eval("var b = " + s);                     // parse it to a var b
var vm = new viewmodel(b);                // pass var b to the viewmodel

This works, but I end up with an intermediate object b before I can use the parsed object

So I tried:

var s = "{ a: new Date(1400000000000) }"; // from a server, can't change this
var vm = new viewmodel(eval(s));          // parse the object and pass to viewmodel

but that doesn't work

Can you explain why?

Is there another way to do this without the need for an intermediate var b?

Joep Geevers
  • 567
  • 4
  • 18

4 Answers4

4

Try to use in this way

var vm = new viewmodel(eval('(' + s + ')')); 
Arjit
  • 3,290
  • 1
  • 17
  • 18
2

You need to wrap the object expression (it's not a valid statement alone):

var s = "{ a: 3, b: 7 }";
var obj = eval('(' + s + ')');

Needless to say that eval is not secure, but if you have no choice... Just be aware that this will execute any code the server sends to you. c.f. Why is using the JavaScript eval function a bad idea? for a more thorough discussion on the matter


Extra fun: try entering both { a: 3, b: 7 } and ({ a: 3, b: 7 }) in the online esprima parser :) http://esprima.org/demo/parse.html

Community
  • 1
  • 1
Aegis
  • 1,749
  • 11
  • 12
0

Another way to do this is:

var s = "{ a: new Date(1400000000000) }";
var vm = new viewmodel(new Function('return ' + s).call());  

The difference between using eval is that this way the code (s) cannot access local variables because the code runs in a separate function scope.

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • I am curious; what would be the performance cost of wrapping the whole thing in a function instead of a StatementExpression? – Aegis Mar 13 '14 at 10:50
  • Did not mean to downvote this. I can't change my vote now since 10 hours has elapsed. I prefer this approach to the dreaded 'eval' and sure would like to upvote it. – TaeKwonJoe Mar 08 '16 at 20:55
-1

The best way to parse JSON is to use JSON.parse(). eval() is not very secure.

var vm = new viewmodel(JSON.parse(s));
Oscar Paz
  • 18,084
  • 3
  • 27
  • 42