I've been working on this issue for a few days now with no progress.
My goal is to convert a List object into JSON for use with Google Analytics' e-commerce service, requiring it to be passed to JavaScript first.
However, I'm encountering an error with the JSON output.
Initially, I have defined an Item class.
Item {
Id,
Name,
Sku,
Price,
Quantity
}
Within my cart class, there is a List of Items.
public List<Item> Items;
To serialize the list, I am using the following code.
var jsonList = JavascriptSerializer.Serialize(cart.Items);
The resulting jsonList is then passed to JavaScript using Razor as follows -
<script type="text/javascript">
var items = @jsonList;
</script>
After rendering in the browser, the generated result appears 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...}]
I am seeking advice on how to remove the " and replace them with quotation marks instead. Could the issue lie within my Item class or my JavaScript?
I have attempted to use @Html.Raw(items) without success - it returns an empty JSON object.