开发者

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

[{&quot;SKUID&quot;:3,&quot;ExFactoryPrice&quot;:765.00},{&quot;SKUID&quot;:5,&quot;ExFactoryPrice&quot;: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.

[{&quot;SKUID&quot;:3,&quot;ExFactoryPrice&quot;:765.00},{&quot;SKUID&quot;:5,&quot;ExFactoryPrice&quot;:39.91}]


Should be:

[{"SKUID":3,"ExFactoryPrice":765.00},{"SKUID":5,"ExFactoryPrice":39.91}]

The simple thing in this case would be to replace &quote; with "

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜