Json Serialization Error in asp.net mvc
Its asp.net mvc paradigm. I have a Prices property on model that is an IList
of type VMPrice
. In view I do something like
<%var serializer = System.Web.Script.Serialization.JavaScriptSerializer();%>
<script type="text/javascript">
var prices = '<%:serializer.Serialize(Model.Prices)%>';
alert(prices);
prices = $.parseJSON(prices); // This li开发者_如何学Gone throws exception Invalid Json object
</script>
The alert call on fourth line shows following string
[{"SKUID":3,"ExFactoryPrice":765.00},{"SKUID":5,"ExFactoryPrice":39.91}]
What's the problem here and how to get around this?
try using "<%=%>"
instead of <%: %>
so that it does not html encode it
also see this SO answer c# to json not rendering properly in view
You need to unescape the string before parsing it.
[{"SKUID":3,"ExFactoryPrice":765.00},{"SKUID":5,"ExFactoryPrice":39.91}]
Should be:
[{"SKUID":3,"ExFactoryPrice":765.00},{"SKUID":5,"ExFactoryPrice":39.91}]
The simple thing in this case would be to replace "e;
with "
精彩评论