简体   繁体   English

解析JSON字符串

[英]Parse JSON string

I have a hard-coded JSON string. 我有一个硬编码的JSON字符串。

var test = "JSON STRING HERE";

I am using jQuery. 我正在使用jQuery。 I know there is a function like getJSON, but that makes an AJAX call. 我知道有一个像getJSON这样的函数,但这会产生一个AJAX调用。 I want it to parse the hardcoded string so that I can use $.each(test, function(a,b){})) 我希望它解析硬编码字符串,以便我可以使用$.each(test, function(a,b){}))

Thank you for your time. 感谢您的时间。

Original Question: 原始问题:

jQuery makes a point of not including a publicly accessible JSON parser or encoder. jQuery指出不包括可公开访问的JSON解析器或编码器。 They want you to use a 3rd party library for that. 他们希望您使用第三方库。

I recommend the one hosted at json.org: 我推荐在json.org上托管的那个:

https://github.com/douglascrockford/JSON-js/blob/master/json2.js https://github.com/douglascrockford/JSON-js/blob/master/json2.js

Alternatively, you could use the jQuery-JSON plugin hosted on Google Code: 或者,您可以使用Google Code上托管的jQuery-JSON插件:

http://code.google.com/p/jquery-json/ http://code.google.com/p/jquery-json/


In answer to "why doesn't jQuery make it's parser available?": 回答“为什么jQuery不能使它的解析器可用?”:

jQuery doesn't actually include a parser of any sort. jQuery实际上并没有包含任何类型的解析器。 In the AJAX section of jQuery's codebase, you can actually see what they do on lines 572-579 . 在jQuery代码库的AJAX部分,您可以实际看到他们在第572-579行上做了什么。

The quick version is that they actually do a check to see if you've included an external JSON library such as the one from json.org, and if it finds it, they use that to parse. 快速版本是他们实际检查你是否包含了一个外部JSON库,如json.org中的那个,如果找到它,他们会用它来解析。 If you have not included one, they return the json wrapped in a function, effectively returning it for evaluation. 如果你没有包含一个,它们会返回包含在函数中的json,有效地返回它进行评估。 Very tricky, but very smart! 非常棘手,但非常聪明!

Do you people realize, that JSON means "JavaScript Object Notation"? 你是否认识到,JSON的意思是“JavaScript Object Notation”? If you have JavaScript Object Notation hardcoded, then just loose the quotes and you're done with parsing it as JavaScript parser will take care of that. 如果你有JavaScript对象表示法硬编码,那么只需松开引号就可以解析它,因为JavaScript解析器会解决这个问题。

var jsonstr = "{prop1: 'val1', prop2: 'val2'}";
var parsed = {prop1: 'val1', prop2: 'val2'};

Easy, wasn't it?!! 容易,不是吗?!! There might be something I'm not aware of, but for me it is quite difficult to understand people writing parsers in JavaScript for JavaScript... Sure if you're not sure about the security of source your jsonstr comes from, then evaluating it straight out of the box might not be the best idea, but in case you and only you control the source and especially if you're hardcoding it like it was said in the question, then just loose the quotes! 可能有一些我不知道的东西,但对我来说很难理解人们用JavaScript编写解析器的JavaScript ...当然如果你不确定你的jsonstr来源的安全性,那么评估它开箱即用可能不是最好的主意,但万一你和你只控制源码,特别是如果你像问题中所说的那样对它进行硬编码,那么就松开引号!

以下是当数据类型设置为“json”时jQuery在ajax请求中执行的操作(这是getJSON所做的,在引擎盖下):

window["eval"]("(" + data + ")");

Unfortunately jQuery doesn't support JSON parsing outside of the AJAX functions with JSON or JSONP as datatype (deserialization si very tightly bound to the AJAX and Callback code). 不幸的是,jQuery不支持使用JSON或JSONP作为数据类型的AJAX函数之外的JSON解析(反序列化与AJAX和Callback代码紧密绑定)。 You can of course just do a 你当然可以做一个

var obj = eval(test);

But this is definitely not the recommened way (except for when you know for sure that your string is only a JavaScript object and not arbitrary source code to be executed). 但这绝对不是推荐的方式(除非你确定你的字符串只是一个JavaScript对象,而不是任意的源代码)。 So the best way is probably to use another library like the JSON2 library (found on json.org ). 因此,最好的方法可能是使用另一个库,如JSON2库 (可在json.org找到 )。

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

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