简体   繁体   English

无效的属性ID错误-List <> c#ASP.NET中的JSON对象

[英]Invalid property ID error - JSON object from List<> c# ASP.NET

I've been on this for a couple of days now and getting nowhere. 我已经在这上呆了几天了,却一无所获。

I'm trying to serialize a List object to JSON so it can be passed to Google Analytics' e-commerce service, so it needs to get passed into javascript beforehand. 我正在尝试将List对象序列化为JSON,以便可以将其传递给Google Analytics(分析)的电子商务服务,因此需要事先将其传递给javascript。

However, there is an error with the json output. 但是,json输出存在错误。

First of all I have an Item class. 首先,我有一个Item类。

Item {
    Id,
    Name,
    Sku,
    Price,
    Quantity
}

My cart class contains a List of Items 我的购物车类别包含商品清单

public List<Item> Items;

I'm using the following to serialize the list. 我正在使用以下序列化列表。

var jsonList = JavascriptSerializer.Serialize(cart.Items);

jsonList is then passed into javascript using Razor like so - 然后使用Razor将jsonList传递到javascript中-

<script type="text/javascript">
    var items = @jsonList;
</script>

The result that is generated in the browser looks like this: 在浏览器中生成的结果如下所示:

items = [{&quot;Id&quot;:ITEM_ID,&quot;Name&quot;:&quot;ITEM_NAME&quot;,&quot;Sku&quot;:&quot;ITEM_SKU&quot;,&quot;Quantity&quot;:ITEM_QTY,&quot;Price&quot;:ITEM_PRICE},{&quot;Id&quot;:ITEM2_ID,&quot;Name&quot;:&quot;ITEM2_NAME&quot;,etc...}]

So I'd like to know how I get rid of the &quot and replace them with the required " instead. Does it have something to do with my Item class or my javascript? 因此,我想知道如何摆脱&quot;并将其替换为要求的&quot;。它与我的Item类或JavaScript有关系吗?

I've tried @Html.Raw(items) and no luck - returns an empty json object. 我已经尝试过@ Html.Raw(items),但没有运气-返回一个空的json对象。

您可以使用Html.Raw返回未编码的HTML

var items = @Html.Raw(@jsonList);

Found a solution that worked on the following post. 找到了适用于以下帖子的解决方案。

How do I write unencoded Json to my View using Razor? 如何使用Razor将未编码的Json写入视图?

Thanks to @James for mentioning unencoded HTML - that put me on the right path. 感谢@James提到了未编码的HTML-这使我走上了正确的道路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Android将JSON对象发送到ASP.net C# - Sending json object to ASP.net C# from Android 如何使用JSON.NET(C#/ ASP.NET)遍历列表并将其转换为JSON对象 - How to iterate through a List and convert it to a JSON object using JSON.NET (C# / ASP.NET) 如何在VB.Net/C#和ASP.NET的下拉列表中显示对象属性 - How to display an object property in a drop down list in VB.Net/C# and ASP.NET ASP.NET MVC中的模型ID属性null c# - Model ID property null in ASP.NET MVC c# ASP.NET MVC C# - 显示 class 属性名称而不是 Z0ECD11C1D7A2877401D148A2DZ 中的属性名称 - ASP.NET MVC C# - Display class property names instead of the property names from JSON 使用C#将JSON存储在ASP.NET文字中并使用JavaScript访问它会引发“无效字符”错误 - Storing JSON in an ASP.NET literal using C# and accessing it using JavaScript throws a “invalid character” error 如何在ASP.NET MVC中为Json对象指定C#属性? - How to specific C# property to Json object in ASP.NET MVC? C#无效的对象名称ASP.NET - C# Invalid object name ASP.NET asp.net c# linq 根据来自另一个列表的 id 从模型生成列表 - asp.net c# linq generate list from model base on id from another list C#ASP.Net应用程序中的“回调错误:来自服务器的无效响应” - “Callback Error: Invalid response from server” from C# ASP.Net application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM