views:

80

answers:

2

Environment: ASP.net MVC:

Given anonymous structure as such:

 var test = new
 {
   name = "me",
   age = "100"
  };

that then gets parsed as

result = Json(test)
data = result.Data // Comes back with { name = "me", age = "100" }

Which then gets successfully passed into a JS function, how do I go about using that as a JSON object, so that I can do something like

function(data) // Where data = { name = "me", age = "100" } or similar
{
 var name = data.name // "me"
}
+1  A: 

Try

var object = eval('(' + data + ')');

then you should be able to do object.name.

Damien
Nope, it doesn't work, even when using eval(data) - it says "me" is undefined and the VS debugger shows eval breaking on { name = me, age = 100 }, without the quotes.
Rio
Are you sure the JSON isn't { "name": "me", "age": 100 }?
David Andres
No. I think part of the issue is with result = Json(test).
Rio
can you post the client side code which retrieves the data?
Damien
Actually. Post what the response is from the server(action). So we can see the Json string?
Damien
A: 

The JSON is invalid, it should be

{
    "name" " "me",
    "age" : "100"
}

And new {..} doesn't do anything meaningful -- the object literal is alone sufficient.

olliej