views:

1209

answers:

2

I went to http://www.json.org/js.html and downloaded the json2.js, thinking i'd be fine, afterall that site is on the top in a google search for 'json javascript' - also they have this really cool url :)

So i've been working a bit with it and it seemed fine, but now i start running into trouble with it - it simply won't parse certain stuff i encode with Newtonsoft's JSON .NET serializer. Ok so perhaps the .net seralizer messes up? Not how i see it - it produces a fine javascript string that looks like perfect json.

The problem comes when it has to encode a single quote ' and perhaps double quotes ". Take a look at these examples (only parts of the full string)

{"Id":10651,"Text":"\'69"}
{"Id":184,"Text":"13\""}

Am I missing something? it's part of a bigger string and all put in a javascript variable like this

var jsonObject = '[{"Id":46,"Type":2,.....................

I'm thinking it has to escape the singlequote in the string to avoid conflicting with my wrapping of the string in single quotes, and escape the double quote to avoid conflicting with the json format?

So either i'm doing something wrong or the json2.js is not doing it right? Or yeah perhaps the .net json is messing up - i'm kinda thinking i'm messing it up, but i've been trying to do all sorts of stuff to help with the parsing like escaping/unescaping etc. before the serializing/deserializing.

A: 

This may be a lead...

http://binnyva.blogspot.com/2006/10/invalid-json.html

And if you are serializing, perhaps protect yourself by serializing to ' and from '.

torial
But the json is with double quotes, the single quotes i use is for defining the json string. Luckily i don't have to (like in c# for example) express my string in double quotes (or i'd have to escape every single double quote in the json! phew :)
Per Hornshøj-Schierbeck
I thought about that too, but shouldn't that be the job of the .net json serializer and the javascript json deserializer? I mean i use premade objects so i don't have to do the (de)serializing myself.
Per Hornshøj-Schierbeck
+1  A: 

Ok i solved the problem. Actually the hint Joel gave me in the comment on my question to try and eval it instead lead me to thinking i can trust this json i'm trying to parse/eval, and since i know it at the time of building the page, why not hardcode it into the webpage AS an object - no escaping of quotes or anything and no evaluating and best of all - no strings :P

So thanks to both you Joel and you torial :)

Per Hornshøj-Schierbeck