JSON字符串中的反斜杠 [英] Backslashes in JSON string
问题描述
我不熟悉这种格式:
{"d":"{\"Table\":[{\"pCol\":12345,\"fCol\":\"jeff\",\"lCol\":\"Smith\",\"dId\":1111111,\"tDate\":\"\\/Date(1153033200000-0700)\\/\"}]}"}
我正在使用 Newtonsoft 序列化我要返回的数据集从我的ASP.Net Web服务.上面的JSON字符串是Firebug返回的内容.我已经使用jsLint检查了此JSON,它很好.
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.
在萤火虫中,我看到了JSON数据和我的第一个alert('success');
.但是,当我尝试alert(msg.d.Table);
时,我什么也没得到.不是警报框或Firebug中的错误...我认为这与这些反斜杠有关...但我不确定.
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.
有什么想法吗?
推荐答案
那些反斜杠是转义字符.它们在与d
关联的字符串中转义了双引号.您不能alert
msg.d.Table
的原因是因为d
的值是一个字符串.您必须使用JSON.parse
将该JSON字符串解析为JSON对象.
然后,您必须将Table
转换回字符串以发出警报.
像这样:
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));
这篇关于JSON字符串中的反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!