i want to pass object json string to javascript function but facing some error. please help. thanks in advance. i'm using MVC5,
my code as below
inside .cshtml
<a href="javascript:void(0);" onclick="addToOrder('@JsonConvert.SerializeObject(item)')">@item.NAME</a>
my json value is inside addToOrder() function looks like,
{
"ITEM_ID": 1,
"NAME": "PEPPER POPPERS",
"FOOD_TYPE": "VEG",
"SIZES": [
{
"SIZE": "FULL",
"PRICE": 220.00
},
{
"SIZE": "MEDIUM",
"PRICE": 170.00
},
{
"SIZE": "8\"",
"PRICE": 50.00
},
{
"SIZE": "12\"",
"PRICE": 40.00
}]
}
throw error when JSON.parse "SIZE": "8\"" in javascript function!
Error in browser console
Uncaught SyntaxError: Unexpected string in JSON at position 37
at JSON.parse (<anonymous>)
at addToOrder (restaurantCounter.js:130)
at HTMLAnchorElement.onclick (1?deptid=6&counterid=1&department=1 AC:933)
Please help. thank you.
"SIZE" : "8\"". You can use this other syntax:"SIZE" : '8"'. The problem is that the function doesn't recognize the\"symbol and treats it as a normal "