69560

bad control character error in json parse

When parsing a JSON object I am getting "bad control character" error in the Firebug console. There are lot of questions and solutions in this site; but I am unable to crack this issue. I believe I am doing something silly. Please point me to my mistake.

JS Fiddle: http://jsfiddle.net/Purus/Eqz2r/

If we change the json data to a plain text instead of html tags, it works.

Error:

SyntaxError: JSON.parse: bad control character in string literal someVal = JSON.parse(sign);

<strong>NOTE:</strong>

    <li>The JSON data is obtained from a PHP function using json_encode function. </li> <li>The json output looks valid in jsonlint</li> </ul>

    Below is the code I have used.

    $(document).ready(function () { var sign = '{"data":"<br\/><br\/>----------<br \/>\r\nFrom Yahoo Team<br \/>\r\n<a href=\"http:\/\/localhost\/base\/1-yahoo-logo.jpg\" target=\"_blank\"><img style=\"padding: 5px;\" src=\"http:\/\/localhost\/plugins\/1-yahoo-logo.jpg\" height=\"120\" width=\"196\" \/><\/a>"}'; someVal = JSON.parse(sign); $(".demo").append(someVal.data); });

    Answer1:

    You need to escape your escapes :)

    Use double \\ instead of \

    http://jsfiddle.net/Eqz2r/2/

    Answer2:

    The problem is the \r and \n. These need to be escaped as

    \\r

    and

    \\n

    in the JSON string

    Answer3:

    var s= JsonString; $.parseJSON(s.replace(/\s+/g,""));

Recommend

  • adding ListView children to a HorizontalScrollView
  • 3.0.0.M1: SSL - Invalid keystore format
  • Is there a way to clone native functions in javascript like window.alert or document.write
  • Msvcr71.dll Msvcp71.dll missing
  • Change device language on Android 6.0 (Android M)
  • How can I let users share their location in Bot Framework webchat channel?
  • cell spacing in div table
  • How to log in to an HTTPS website with Jsoup?
  • HttpURLConnection Closing IO Streams
  • How to 'create temp table as select' in Slick?
  • how does System.Web.HttpRequest::PathInfo work?
  • Parsing a CSV string while ignoring commas inside the individual columns
  • Swing - Get new component under mouseReleased
  • Play WS (2.2.1): post/put large request
  • How to clear text inside text field when radio button is select
  • Launch Runnable Jar from Web Start
  • Different response to non-authenticated users and AJAX calls
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Get object from AWS S3 as a stream
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • MySQL WHERE-condition in procedure ignored
  • Web-crawler for facebook in python
  • Do I've to free mysql result after storing it?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Calling of Constructors in a Java
  • Traverse Array and Display in markup
  • bootstrap to use multiple ng-app
  • Transpose CSV data with awk (pivot transformation)
  • How to get icons for entities from eclipse?
  • trying to dynamically update Highchart column chart but series undefined
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • embed rChart in Markdown
  • Turn off referential integrity in Derby? is it possible?
  • Is it possible to post an object from jquery to bottle.py?
  • JaxB to read class hierarchy
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal