翻译:三分钟学懂JSON
Understanding JSON: the 3 minute lesson
JavaScript Object Notation.
[A ridiculous name. It should be called Lightweight Ecmascript Object Notation, or 'LEON' for short. ;-)]
Json代表着什么?
Javascript Object Notation。
【真是一个可笑的名字。它本该叫做轻量级的核心对象符号,或者简称'LEON'。;-)】
And what does that mean?
JSON is a syntax for passing around objects that contain name/value pairs, arrays and other objects.
Json意味着什么?
JSON是一种在对象之间传递数据的语法,这些对象一般包含name/value对,数组或其他的对象。
Here's a tiny scrap of JSON:
这是一个JSON的小片段:
{"skillz": { "web":[ {"name": "html", "years": "5" }, {"name": "css", "years": "3" }], "database":[ {"name": "sql", "years": "7" }] }}
You got that? So you'd recognise some JSON if you saw it now? Basically:
你看得懂吗?你因此想起之前的一些JSON印象?JSON主要有以下特点:
Squiggles, Squares, Colons and Commas
- Squiggly brackets act as 'containers'
- Square brackets holds arrays
- Names and values are separated by a colon.
- Array elements are separated by commas
花括号,方括号,冒号和逗号
- 花括号可以看作是容器
- 方括号表示数组
- 名字和值用冒号分开
- 数组元素用逗号分开
Think 'XML with Anorexia'
(Or if you're as old as me, think "'.INI' files, with hierarchy.")
(Or if you're a smug lisp weenie, think "S-expressions", and just be smug.)
想想'XML 厌食症'
JSON is like XML because:
- They are both 'self-describing' meaning that values are named, and thus 'human readable'
- Both are hierarchical. (i.e. You can have values within values.)
- Both can be parsed and used by lots of programming languages
- Both can be passed around using AJAX (i.e. httpWebRequest)
JSON和XML有所相同,因为:
- 它们都是'自描述'的,意味着值一般都会有名字,这样就可以让人类读得懂
- 它们都是分层的。(如,可以在值里嵌套另一些值)
- 它们都可以被众多编程语言解析和使用
- 二者都可以使用AJAX传递数据(如httpWebRequest)
JSON is UNlike XML because:
- XML uses angle brackets, with a tag name at the start and end of an element: JSON uses squiggly brackets with the name only at the beginning of the element.
- JSON is less verbose so it's definitely quicker for humans to write, and probably quicker for us to read.
- JSON can be parsed trivially using the eval() procedure in JavaScript
- JSON includes arrays {where each element doesn't have a name of its own}
- In XML you can use any name you want for an element, in JSON you can't use reserved words from javascript
JSON和XML有所不同,因为:
- XML采用尖角括号(<>),在元素的开头和结尾使用一个标签名:JSON采用花括号,而名字只在元素的开头处出现一次
- JSON没那么冗余,因此更加清楚,书写更快,也许也会读得更快
- JSON可以在JavaScript下使用eval()方法就可以从字符串解析成JSON对象
- JSON支持数组(只是数组的每个元素并有没自己的名字)
- 在XML中你能使用任何你想到的名字作为元素名,但在JSON你不允许使用javascript的保留关键字
When you're writing ajax stuff, if you use JSON, then you avoid hand-writing xml. This is quicker.
在你写ajax的东西的时候,如果使用JSON,那你将不要手写任何xml代码。这样子写代码很快捷。
Again, when you're writing ajax stuff, which looks easier? the XML approach or the JSON approach:
此外,当你又在编写关于ajax的东西,哪一个看起来更简单一些呢:XML的方法和JSON的方法?
The XML approach:
- bring back an XML document
- loop through it, extracting values from it
- do something with those values, etc,
versus
The JSON approach:
- bring back a JSON string.
- 'eval' the JSON
XML方式:
- 获取XML文档
- 遍历这个文档,从中获取一些值
- 对这些值进行处理
对决
JSON方式:
- 获取JSON字符串
- 解析成JSON(调用eval方法)
So this is Object-Oriented huh?
哈,那JSON可以因此而称作面向对象吗?
Nah, not strictly.
嗯,不完全是。
JSON is about as object oriented as VB6. It provides a nice encapsulation technique, that you can use for separating values and functions out, but it doesn't provide anything inheritence, polymorphism, interfaces, or OO goodness like that.
JSON的面向对象就像VB6的面向对象一样。它提供了一个很好的封装机制,可以用来区分不同的值和不同的函数,但它不支持继承、多态、接口和面向对象的其他精华等。
It is certainly a step in the right direction though, making javascript easier to maintain, share and reuse.
这绝对是走向正确方向的坚实一步,使得javascrip更易于维护、共享和重用。
Thomas Frank wrote a nifty little javascript library called classyJSON for adding inheritance and scoping capabilities to JSON code.
托马斯·弗兰克写了小巧漂亮的javascript库叫做classyJSON,这个库使JSON代码支持继承和动态作用域。
And it's just for the client-side right?
这只针对于客户端有效吗?
Yes and no. On the server-side you can easily serialize/deserialize your objects to/from JSON. For .net
programmers you can use libraries like Json.net to do this automatically for you (using reflection i assume), or you can generate your own custom code to perform it even faster on a case by case basis.
是的也不是的。在服务器端,可以从JSON序列化或反序列化一个对象。对于.net程序员来说,可以使用Json.net库,这个库会为你自动处理这些事情(在我假设使用映射的前提下),或者你也可以根据不同的情况来生成你自定义的代码来完成这件事情,这样甚至会执行得更快。
Three minutes is nearly up....
三分钟快过去了……
As near as a I can tell, JSON was invented by a guy called Douglas Crockford. Read his website if you want, he's pretty funny.
据我所知,道格拉斯·克洛克福特发明了JSON。感兴趣的话,去看看他的网站吧,他真是一个有趣的人呵。
Now Go And Read Someone Who Knows
现在你可放开去读一些懂JSON的人写的东西了
(Retrieved from Delicious using JSON!)
(从网站"美味的JSON"搜刮过来的)
- DOM Query Speed Test
- 24 ways: Don't be eval()
- Understanding JSON: the 3 minute lesson
- Serializing Objects as JavaScript using Atlas, JSON.NET and AjaxPro
- JSON - Wikipedia, the free encyclopedia
- Introduction to JSON
- XML.com: JSON and the Dynamic Script Tag: Easy, XML-less Web Services for JavaScript
- Classy JSON
- ajax json tutoral
- XML to JSON - a converter
That's all.
本文完。
I've only tinkered with this stuff for a few minutes -- so I've probably said some completely wrong things. If so, please leave a comment, telling me what an idiot i am. I'll be happy to correct any specific mistakes. Best of luck!
我只花了一些时间在这篇文章上面,因此,也可能顺口就说出了完全错误的东西。如果这样,请给我留言,告诉我是一个多么大的傻瓜。我很愿意修改任何一个细节上的错误。祝你好运!
(Side Note:If you replace { and } with "<" and "/>", and you replace ":" with "/"... you'd have something very similar to gaXml. Funny old world.
(注:如果你把"{"和"}"替换成"<"和"/>",并把":"换成"/"……你将得到一个和gaXml(?)非常相似的东西。过去的那个gaXml世界也是多么有趣啊。
(Other side note: Jason and Ajax were both mythical greek heroes. Prediction: other forthcoming technology jargon will include: Heracles, Perseus, Deucalion, Theseus and Bellerophon.)
(又注:Jason(伊阿宋)和Ajax(阿加克斯)都是希腊神话中虚构的英雄。在这里顺便预言一下:接下来新出现的技术将会包括:赫拉克勒斯(Heracles)、珀尔修斯(Perseus)、丢卡利翁(Deucalion)、提修斯(Theseus )和柏勒罗丰(Bellerophon)。)