1

I'm using a custom JavaScriptConverter to serialize two objects. One of them is a list of objects and it works just fine; the other one is a single object. I serialize both and put them in the source code of the page. When I look at them HTML source in a browser, I see that the one that works looks like this:

var Object1 = '[{....}]'; 

while the one that doesn't work looks like this:

var Object2 = '{...}'; 

When I run an eval, it doesn't work with Object2. I'm just not seeing why the serialization is different since I'm using the same principal for both; I'm obviously doing something wrong. If you've run into a similar issue or have a suggestion, then please let me know.

Thanks.

1
  • Why don't you put the single object inside a list? It should output JSON similar to Object1. Commented Mar 21, 2011 at 21:49

3 Answers 3

1

You are running into a Javascript parsing ambiguity.

Instead of:

eval(json) 

you need:

eval('(' + json + ')') 

In Javascript, an open brace can start either an object literal:

{ a: 0, b: 1 } 

or a block:

{ var a = 3; f(a); } 

However, a block cannot appear in an expression, so adding the parentheses resolves the ambiguity.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm still getting a null: DayStats = eval('(' + Object2 + ')');
ok, thanks, that actually fixed it. Could you explain why eval(Object1); works but eval(Object2); doesn't.
1

Section 12.4 of the spec says:

An ExpressionStatement cannot start with an opening curly brace because that might make it ambiguous with a Block. Also, an ExpressionStatement cannot start with the function keyword because that might make it ambiguous with a FunctionDeclaration.

There are numerous workarounds/solutions discussed here:

Comments

0

There is nothing weird. In JSON you have normal objects which are denoted with { ... } and lists of normal objects which are denoted with [{ ... }, { ... }, ...] and . So if your second object is not a list you cannot expect it to be serialized as such. If you wanted your second object to be enclosed with [] you could create a list containing one element which is the second object and then serialize this list.

4 Comments

ok, so why is the eval not working? I'm writing MyPageObject = eval(Object2);
@frenchie, why are you doing an eval? You already have a JSON object so simply remove the simple quotes and evals and once you have var Object2 = { 'foo': 'bar' }; you could directly alert(Object2.foo);.
Because the page calls back the server for other objects based on input and I'm using these objects to build HTML on the fly. All objects go through the same process; it just happens that the first object is sent directly in the page from the server.
@frenchie, I don't understand your scenario but all I can say is that if you are using eval then whatever you are doing, you are doing it wrong and there is a better way :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.