0

I am trying to store a JSON string inside an ASP.NET Literal Control. This JSON string is in this format:

[
    ['Time', 'Speed', 'Distance'],
    ['09-07-2015 2:21:39 PM', 76, 500],
    ['08-07-2015 6:02:41 PM', 350, 500],
    ['08-07-2015 5:55:43 PM', 10, 50],
    ['08-07-2015 5:55:36 PM', 50, 50],
    ['08-07-2015 5:55:21 PM', 180, 80],
    ['08-07-2015 5:51:38 PM', 100, null]
]

This is how I am adding it to literal:

Literal1.Text = "<input type='hidden' id='jsondata' value=\"" + JSONData + "\" />";

But actual JSON data that will be entered by the user may contain any character including " and '. And this JSON data is accessed inside JavaScript like:

function SetGraphData() {
    var hiddenField = $('#jsondata');
    jsonData = $.parseJSON(hiddenField.val());
}

But when parsing JSON, it's showing me Invalid character error. Please tell me what can be the reason?

1
  • 4
    JSON strings must be quoted with double-quote characters. Commented Jul 10, 2015 at 21:20

1 Answer 1

2

JSON uses double quotes, not single quotes.

Also you should make sure to properly HTML escape your JSON data using something like HttpUtility.HtmlEncode, or since you're using ASP.NET, just use <asp:HiddenField> instead of your current contraption.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I used HtmlEncode and replaced single quotes with double quotes. It's working now. :)

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.