1

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.

3
  • "SIZE": "8\"" There is an extra " Commented Mar 16, 2018 at 11:53
  • 1
    I think the problem is that you have a field "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 " Commented Mar 16, 2018 at 11:54
  • Are single quotes valid for JSON? I thought the specification explicitly said double quotes Commented Mar 16, 2018 at 12:23

1 Answer 1

3

The problem is with the json itself. Try this one

{
        "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
        }]
    }
Sign up to request clarification or add additional context in comments.

4 Comments

A part from your fix, that is right, I think that since the OP gets a JSON.parse "SIZE": "8\"" error that means that something else is wrong. You can see my comment on the OP post
But json is dynamically generated while .cshtml compile. and my SIZE in list is may be anything. it may be Small,Full,8",12",4",etc. how to manage? @Eugenio
@Pravin right now the problem occurs because you use " to define the field of your json but you also use it as the string inside it so the program goes in an error state because it can't understand which is the closing one. You can just bypass that by using ' for your field and still use " as the string, like this: "SIZE" : '8"'. That way the program understands that the string starts when it finds a ' and ends at another '. Is this clear enough?
@Eugenio you are correct. The backslash will be an escape character to the JavaScript parser, not the JSON parser.So JSON.parse('{"SIZE":"8\""}') will be turning into a invalid json. So try with escaping it JSON.parse('{"SIZE":"8\\""}').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.