简体   繁体   English

适用于Java的Json库?

[英]A working Json library for Javascript?

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 :) 我去了http://www.json.org/js.html并下载了json2.js,以为我会很好的,毕竟该网站在Google搜索“ json javascript”的顶部–他们也有这真的很酷的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. 因此,我一直在使用它,它看起来还不错,但是现在我开始遇到麻烦了-它根本无法解析使用Newtonsoft的JSON .NET序列化器编码的某些内容。 Ok so perhaps the .net seralizer messes up? 好的,也许.net seralizer搞砸了? Not how i see it - it produces a fine javascript string that looks like perfect json. 不是我所看到的-它产生了一个看起来像完美json的很好的javascript字符串。

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 它是一个更大的字符串的一部分,并全部放入这样的javascript变量中

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? 我在想它必须转义字符串中的单引号以避免与我的单引号中的字符串换行冲突,并转义双引号以避免与json格式冲突?

So either i'm doing something wrong or the json2.js is not doing it right? 所以我做错了什么,或者json2.js做错了吗? 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. 或者是的,.net json可能搞砸了-我有点想把它搞砸了,但是我一直在尝试做各种事情来帮助进行解析,例如转义/取消转义等,然后进行序列化/反序列化。

This may be a lead... 这可能是领先...

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

And if you are serializing, perhaps protect yourself by serializing to ' 并且,如果您要序列化,也许可以通过序列化为'来保护自己' and from ' 并来自' .

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 实际上,乔尔(Joel)在我的问题评论中给我的提示是尝试评估它,反而使我想到我可以信任我要解析/评估的json,并且由于我在构建页面时就知道了,为什么不会将其作为对象硬编码到网页中-不会转义引号或其他内容,也不会评估,而且最重要的是-无需字符串:P

So thanks to both you Joel and you torial :) 因此,感谢您Joel和您的讲解:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM