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);
精彩评论