1

I have the following JSON string:

{"d":"{\"Foo\":7,\"Bar\":5,\"Foobar\":3}"}

And the corresponding invocation in my js file:

$.getJSON("Foo.svc/GetSomeFoo", function (response) {
            alert(response["Foo"]);
            alert(response["Bar"]);
            alert(response["Foobar"]);
        });

Just trying to write out the values, but can't seem to get it out. It is probably very simple, but I am not finding anything helpful while googling it.

4 Answers 4

4

Your JSON has embedded JSON. You need to do:

var d = JSON.parse(response.d);
alert(d.Foo);
...
Sign up to request clarification or add additional context in comments.

Comments

1

Since you have an object named d as the outer object, you will need to get your data via it.

For ex: response.d["Foo"]

Comments

1

Try this:

alert(response.d["Foo"]);

response gives you this: {"d":"{\"Foo\":7,\"Bar\":5,\"Foobar\":3}"}
response.d will give you: {\"Foo\":7,\"Bar\":5,\"Foobar\":3}
and finally response.d["Foo"] or response.d.Foo will give you: 7

1 Comment

Ahhh so simple it was. the 'd' is something new since last I messed with json a couple of years ago. Thanks.
1

You can do this -

 alert(response.d.Foo);

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.