1

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

2 Answers 2

2

Try

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

then you should be able to do object.name.

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

4 Comments

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.
Are you sure the JSON isn't { "name": "me", "age": 100 }?
No. I think part of the issue is with result = Json(test).
Actually. Post what the response is from the server(action). So we can see the Json string?
0

The JSON is invalid, it should be

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.