0

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 string on server side. How can I parse this string to JSON?

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

the removal of the quotes is key.
i tried it without quotes but it complains that data.0 is null or not an object
sorry i was still trying to $.parseJson. it is perfectly working now. thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.