开发者

$.parseJson with html encoded string

开发者 https://www.devze.com 2023-04-04 09:36 出处:网络
I receive an html encoded string from server code and I want to convert it to JSON using $.parseJSON but it throws the exception. This is the string returned by asp.net\'s JavaScript serializer:

I receive an html encoded string from server code and I want to convert it to JSON using $.parseJSON but it throws the exception. This is the string returned by asp.net's JavaScript serializer:

{"Property":"Name","Template":"\u003cinput data-val=\"true\" data-val-number=\"The field ID must be a number.\"....

String is correct as returned by JS serializer but when I call

var data = '<%=serializer.Serialize(Model))%>';
data = $.parseJson(data);
// I also tried $.parseJSON(unescape(data)) but with no luck

The situation is that I can't prevent html encoding of st开发者_运维百科ring on server side. How can I parse this string to JSON?


Since JSON is basically just literal JS code for defining a variable's contents, you could just skip the whole json parseing step with:

var data = <%= serializer.Serialize(Model)) %>; // note: no quotes
alert(data.Property);
0

精彩评论

暂无评论...
验证码 换一张
取 消