Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backslashes in JSON string

I'm not familiar with this format:

{"d":"{\"Table\":[{\"pCol\":12345,\"fCol\":\"jeff\",\"lCol\":\"Smith\",\"dId\":1111111,\"tDate\":\"\\/Date(1153033200000-0700)\\/\"}]}"}

I'm using Newtonsoft to serialize my DataSet that I'm returning from my ASP.Net webservice. The above JSON string is what Firebug is returning. I have checked this JSON using jsLint and it is good.

In firebug I see the JSON data and my first alert('success'); However when I try to alert(msg.d.Table); I get nothing. Not an alert box or an error in Firebug... I think it has something to do with these backslashes... But I'm not sure.

Any ideas?

like image 228
webdad3 Avatar asked Jan 21 '23 03:01

webdad3


2 Answers

Those backslashes are escape characters. They are escaping the double quotes inside of the string associated with d. The reason you cant alert msg.d.Table is because the value of d is a string. You have to use JSON.parse to parse that JSON string into a JSON object. Then, you have to convert Table back to a string to alert it. Something like this:

var dObj = JSON.parse(msg.d);
alert(JSON.stringify(dObj.Table, null, 2)); 
like image 155
Alex Avatar answered Jan 28 '23 23:01

Alex


The ASP.Net webservice is already serializing the return value to JSON. (in a d property for security reasons)

When you return pre-serialized JSON data, it thinks you're giving it a normal string, and proceeds to serialize the string as JSON.

Therefore, you get a JSON object with a d property that contains the raw JSON string (with correctly escaped quotes) that you returned.

You should return the raw object and let ASP.Net serialize it for you instead of serializing it yourself.

like image 26
SLaks Avatar answered Jan 28 '23 23:01

SLaks