我想将对象json字符串传递给javascript函数但面临一些错误。请帮忙。提前致谢。我正在使用MVC5,
我的代码如下
内.cshtml
<a href="javascript:void(0);" onclick="addToOrder('@JsonConvert.SerializeObject(item)')">@item.NAME</a>
我的json值在addToOrder()函数里面,
{
"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
}]
}
当JSON.parse&#34; SIZE&#34;:&#34; 8 \&#34;&#34;时抛出错误在javascript函数中!
浏览器控制台错误
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)
请帮忙。谢谢。
答案 0 :(得分:3)
问题在于json本身。试试这个
{
"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
}]
}