Reputation: 575
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.
However, there is an error with the json output.
First of all I have an Item class.
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 -
<script type="text/javascript">
var items = @jsonList;
</script>
The result that is generated in the browser looks like this:
items = [{"Id":ITEM_ID,"Name":"ITEM_NAME","Sku":"ITEM_SKU","Quantity":ITEM_QTY,"Price":ITEM_PRICE},{"Id":ITEM2_ID,"Name":"ITEM2_NAME",etc...}]
So I'd like to know how I get rid of the " and replace them with the required " instead. Does it have something to do with my Item class or my javascript?
I've tried @Html.Raw(items) and no luck - returns an empty json object.
Upvotes: 1
Views: 2198
Reputation: 575
Found a solution that worked on the following post.
How do I write unencoded Json to my View using Razor?
Thanks to @James for mentioning unencoded HTML - that put me on the right path.
Upvotes: 1