Scott Selby
Scott Selby

Reputation: 9570

Jquery parsing JSON response

I have a jQuery function - this is the response that is it is getting

   {"d":"{\"res\":\"\\u003cdiv class=\\\"accordian_head\\\"\\u003e\\u003cdiv class=\\\"plus\\\"\\u003e\\u003c/div\\u00..........."NewTags\":\"\\........

when the response is just {"d":" response..." } I have no trouble reading it in jQuery with just msg.d - but here I have "res" : "text" nested in "d" , so how to I read res out of this response ? I tried

      success: function (msg) {
                    var obj = unescape(msg.d);
                    var res = unescape(obj.res);
                    var newtags = unescape(obj.NewTags);

Where the sample JSON I pasted is msg

Upvotes: 2

Views: 616

Answers (3)

Andy
Andy

Reputation: 30135

You'll have to run msg.d through $.parseJSON().

It would be even better if you could tweak the server script to return it as a nested JSON instead.

Upvotes: 0

Dimitri
Dimitri

Reputation: 7013

when you get d it returns a string. You need an object to then access it's res property. Try this:

success: function (msg) {
                    var obj = eval(unescape(msg.d));
                    var res = unescape(obj.res);
                    var newtags = unescape(obj.NewTags);

Depending on what unescape does, you might want to try just eval(msg.d)

If eval doesn't work, try jQuery.parseJSON

Upvotes: 1

Amir
Amir

Reputation: 1872

How about:

$.parseJSON(msg.d)

This will unescape the quotes and parse the string to JSON.

Upvotes: 2

Related Questions