bala3569
bala3569

Reputation: 11010

What is wrong in this JSON string?

My JSON string looks like this,

[{"id" : "38","heading" : "Can you also figure out how to get me back the 10 hours I
sp.....","description" : "Im having a very similar problem with the Login control - again it always generates a default style containing border-collapse - only in this case .....","img_url" : "~/EventImages/EventImages1274014884460.jpg","catogory" : "News","doe" : "15-05-2010 "},

{"id" : "40","heading" : "How to remove the border of each row (from the gridview cont.....","description" : "How to remove the border of each row (from the gridview control). ? I set borderWidth to 0, and the borders are not displayed with IE, but the top a.....","img_url" : "~/EventImages/EventImages1274028613023.jpg","catogory" : "News","doe" : "15-05-2010 "},

{"id" : "41","heading" : "Realmac Software | How to fix FancyZoom popup? (pops up behi.....","description" : "The first thing we need is an options dialog, not only to make it easier for the user, but also because later we will want to launch this dialog from .....","img_url" : "~/EventImages/EventImages1274037688120.jpg","catogory" : "News","doe" : "15-05-2010 "},

{"id" : "42","heading" : "hi jacon
dsadddaddddddddddddddd","description" : "hi jacon
This is a little messy because the clientHeight/Width properties can mean different things in different browsers, and even different thi.....","img_url" : "~/EventImages/EventImages1274041211533.jpg","catogory" : "News","doe" : "15-05-2010 "}

But get the error,

unterminated string literal....

EDIT:

I used this but it didn't work,

 var newjson = cfreturn( """" & ToString( HfJsonValue ).ReplaceAll( "(['""\\\/\n\r\t]{1})", "\\$1" ) & """" ) ;
 var jsonObj = eval('(' + newjson + ')');

Error: missing ) after argument list

Source Code:

var newjson = cfreturn( """" & ToString( HfJsonValue ).ReplaceAll( "(['""\\\/\n\r\t]{1})", "\\$1" ) & """" ); 

EDIT:

There is a ' mark in JSON string that causes the problem... any suggestion

Upvotes: -1

Views: 6223

Answers (6)

Nathan
Nathan

Reputation: 12314

That stray quote mark should be escaped automatically. Are you generating the json yourself? If so I would strongly recommend using a json library for whatever language you are working with to generate it for you, otherwise you will keep running into increasingly obscure versions of this problem.

Upvotes: 0

Christian Studer
Christian Studer

Reputation: 25637

After playing around with JSONLint, I found the following two problems:

  1. Newlines need to be escaped with \\n.
  2. You're missing the terminal ].

Upvotes: 3

Somnath
Somnath

Reputation: 3275

You can try the example below.

<script type="text/javascript">

        var newjson = "{'id' : '38','heading' : 'Can you also figure out how to get me back the 10 "
        + "hours I sp.....','description' : 'Im having a very similar problem with the "
        + "Login control - again it always generates a default style containing border "
        + " -collapse -only in this case .....','img_url' : '~/EventImages/"
        + "EventImages1274014884460.jpg','catogory' : 'News','doe' : '15-05-2010 '}";

        var jsonObj = eval('(' + newjson + ')');

        alert(jsonObj.id);
        alert(jsonObj.heading);

    </script>

Upvotes: 0

Sky Sanders
Sky Sanders

Reputation: 37104

Wherever you are getting the JSON is generating invalid JSON.

Cleaning it up is less than trivial and is approaching the problem from the wrong direction.

Solve the problem at the source.

Upvotes: 0

Soufiane Hassou
Soufiane Hassou

Reputation: 17750

You should try to escape all special characters including \n which is generally the cause of such error.

Take a look at this blog post

Upvotes: 0

nickf
nickf

Reputation: 546503

It's most likely to be caused by an unescaped linebreak in there somewhere. In the example, there's linebreaks everywhere, though I assume they were added for formatting purposes.

If you do have linebreaks, either replace them with "\n" or put "\" at the end of the line:

var x = "multi \n line \n string";
var y = "multi\
line\
string";

// this causes the "unterminated string literal" error.
var z = "multi
line
string";

Upvotes: 1

Related Questions