21世纪初Douglas Crockford looking for a simple data exchange format that can exchange data between servers。这其实需要二步,第一步是将各种数据转化为一个字符串,也就是数据的串行化 (serialization),第二步才是交换这个字符串。

当时通用的数据交换语言是XML,但是Douglas Crockford觉得XML的生成和解析都太麻烦,所以他提出了一种简化格式,也就是Json。

Json的规格非常简单,只用一个页面、几百个字就能说清楚,而且Douglas Crockford声称这个规格永远不必升级,因为该规定的都规定了。

1) 并列的数据之间用逗号(“,”)分隔。

2) 映射用冒号(“:”)表示。

3) 并列数据的集合(数组)用方括号("[]")表示。

4) 映射的集合(对象)用大括号(“{}”)表示。

上面四条规则,就是Json格式的所有内容。

比如,下面这句话:

“北京市的面积为16800平方公里,常住人口1600万人。上海市的面积为6400平方公里,常住人口1800万。”

写成json格式就是这样:

[
{"城市":"北京","面积":16800,"人口":1600},
{"城市":"上海","面积":6400,"人口":1800}
]

如果事先知道数据的结构,上面的写法还可以进一步简化:

[
["北京",16800,1600],
["上海",6400,1800]
]

由此可以看到,json非常易学易用。所以,在短短几年中,它就取代xml,成为了互联网上最受欢迎的数据交换格式。

我猜想,Douglas Crockford一定事先就知道,数据结构可以简化成三种形式,否则怎么可能将json定义得如此精炼呢!

3.

I remember learning javascript, I was not sure, "array"(array) and the "objects"(object) where the fundamental difference between the two can be used to represent a collection of data

For example there is an array a = [1,2,3,4], there is an object a = {0:1,1:2,2:3,3:4}, and then you run the alert (a [1]) In both cases the result is the same run! This means that data collection can either use an array that can also be expressed by the object, then I do in the end the use of which

I later learned that ordered arrays of data collection, while the object represents a collection of unordered data. If the order of the data is very important to use an array, otherwise the object

4.

当然,数组和对象的另一个区别是,数组中的数据没有“名称”(name),对象中的数据有“名称”(name)。

But the problem is that many programming languages​​, there are called "associative arrays"(associative array) things. This array is the name of the data

比如在javascript中,可以这样定义一个对象:

var a={"城市":"北京","面积":16800,"人口":1600};

但是,也可以定义成一个关联数组:

a["城市"]="北京";
a["面积"]=16800;
a["人口"]=1600;

This increase was initially on the arrays and objects of my confusion, and later came to realize that, in the Javascript language, an associative array is an object, the object is an associative array. This is completely different with the php language, in php, the associative array is an array

比如运行下面这段javascript:

var a=[1,2,3,4];

a['foo']='Hello World';

alert(a.length);

最后的结果是4,也就是说,数组a的元素个数是4个。

但是,运行同样内容的php代码就不一样了:

$a=array(1,2,3,4);

$a["foo"]="Hello world";

echo count($a);

最后的结果是5,也就是说,数组a的元素个数是5个。

posted on 2010-06-15 19:12  myjavawork  阅读(216)  评论(0编辑  收藏  举报