翻译:三分钟学懂JSON

Understanding JSON: the 3 minute lesson

三分钟学懂JSON


Two months ago you'd
never heard of JSONIf you are anything like me (and I fear that you are) then this is your experience with JSON so far:

两个月以前,你可能和我一样听都没听说过JSON(恐怕正是如此),而这将是目前为止你第一次看到有关JSON的资料:

  1. One month ago you'd heard the term but paid no attention
  2. One week ago you'd heard it mentioned a few times and started to think, right... some more crap to learn
  3. Today you woke up with an alarm bell ringing in the back of your mind that said WHAT THE BLOODY HELL IS THIS JSON THING AND WHY IS IT EVERYWHERE ALL OF A BLOODY SUDDEN!

  1. 一个月之前你应该没有听说过这个术语,并不会注意到它
  2. 一个星期之前你应该听说了关于它的一些东西并开始思考:废物一个,有什么值得一学的
  3. 今天你终于被后脑勺的一个警钟惊醒:JSON这东西真是个嗜血的玩意儿,为什么JSON使得到处突然充满一股血腥般地变动之中

Well I had a slow bus ride home tonight (friday is always slow) and i took a pile of "JSON" tutorials with me. So now I can gently lead you through some BabySteps in JSON.

好吧,我今晚正好开一辆慢慢的公车(周五总是慢一点),并一身都是"JSON"教程。因此,此时我可以轻轻地带着你经历你的JSON之路上的蹒跚学步。

here we go...

好戏要开场了……

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

  1. Squiggly brackets act as 'containers'
  2. Square brackets holds arrays
  3. Names and values are separated by a colon.
  4. Array elements are separated by commas


花括号,方括号,冒号和逗号

  1. 花括号可以看作是容器
  2. 方括号表示数组
  3. 名字和值用冒号分开
  4. 数组元素用逗号分开

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 厌食症'

(或者你和我一样老的话,就想想"'.INI'文件的层级风格")
(或者你是个稍微有点Lisp语言基础的书呆子,想想"S表达式"(Lisp语言采用的一种加括弧语法),并仍旧做一个这样的书呆子)

JSON is like XML because:

  1. They are both 'self-describing' meaning that values are named, and thus 'human readable'
  2. Both are hierarchical. (i.e. You can have values within values.)
  3. Both can be parsed and used by lots of programming languages
  4. Both can be passed around using AJAX (i.e. httpWebRequest)

JSON和XML有所相同,因为:

  1. 它们都是'自描述'的,意味着值一般都会有名字,这样就可以让人类读得懂
  2. 它们都是分层的。(如,可以在值里嵌套另一些值)
  3. 它们都可以被众多编程语言解析和使用
  4. 二者都可以使用AJAX传递数据(如httpWebRequest)

JSON is UNlike XML because:

  1. 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.
  2. JSON is less verbose so it's definitely quicker for humans to write, and probably quicker for us to read.
  3. JSON can be parsed trivially using the eval() procedure in JavaScript
  4. JSON includes arrays {where each element doesn't have a name of its own}
  5. In XML you can use any name you want for an element, in JSON you can't use reserved words from javascript

JSON和XML有所不同,因为:

  1. XML采用尖角括号(<>),在元素的开头和结尾使用一个标签名:JSON采用花括号,而名字只在元素的开头处出现一次
  2. JSON没那么冗余,因此更加清楚,书写更快,也许也会读得更快
  3. JSON可以在JavaScript下使用eval()方法就可以从字符串解析成JSON对象
  4. JSON支持数组(只是数组的每个元素并有没自己的名字)
  5. 在XML中你能使用任何你想到的名字作为元素名,但在JSON你不允许使用javascript的保留关键字
But Why? What's good about it?
但是为什么JSON有那么多好处?为什么呢?

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:

  1. bring back an XML document
  2. loop through it, extracting values from it
  3. do something with those values, etc,

versus

The JSON approach:

  1. bring back a JSON string.
  2. 'eval' the JSON

XML方式:

  1. 获取XML文档
  2. 遍历这个文档,从中获取一些值
  3. 对这些值进行处理

对决


JSON方式:

  1. 获取JSON字符串
  2. 解析成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。感兴趣的话,去看看他的网站吧,他真是一个有趣的人呵。

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)。)


引用 : http://secretgeek.net/json_3mins.asp

原文地址:https://www.cnblogs.com/xiaxiazl/p/2439605.html