浅谈JSON和XML
JSON和XML都可作为数据定义的方式,两者在异步传输中都得到了肯定,而他们之间的区别,我想,应该主要在于解析的简单程度。
JSON定义数据的主要方式是{}和[],{}代表的是对象,[]代表的是数组;而XML则是通过<>的分层来表示信息。下面我们就分别看一下这两种不同的方式。
大家都知道,在面向对象的三层构架体系中我们通常将现实对象抽象成实体,同时看重它的属性。例如人有自己的姓名,性别,年龄,工作,喜欢的运动等信息。用JSON 来表示:
var person={
"Name":"NAME",
"Sex":"SEX",
"Age":"AGE",
"Work":[
{"1986-1989":"FirstWork","1989-1999":"SecondWork","1999-2007":"ThirdWork"},
"OtherThings"
]
}
用XML表示:
<Person>
<Name>Name</Name>
<Sex>Sex</Sex>
<Age>Age</Age>
<Work>
<FirstWork>
<Time>1986-1989</Time>
<WorkName>FirstWork</WorkName>
</FirstWork>
<SecondWork>
<Time>1989-1999</Time>
<WorkName>SecondWork</WorkName>
</SecondWork>
<ThirdWork>
<Time>1999-2007</Time>
<WorkName>ThirdWork</WorkName>
</ThirdWork>
<OtherThings></OtherThings>
</Work>
</Person>
当然我们不能仅仅定义数据,最终的目的还是为了将数据呈现给用户,或者提取出来加以利用。JSON定义的数据可以直接通过javascript来访问,毕竟它直接就是javascript的对象。例如这里可以通过person.Name这样的方式来访问数据。XML定义的数据则可以通过DOM解析。
JSON定义数据的主要方式是{}和[],{}代表的是对象,[]代表的是数组;而XML则是通过<>的分层来表示信息。下面我们就分别看一下这两种不同的方式。
大家都知道,在面向对象的三层构架体系中我们通常将现实对象抽象成实体,同时看重它的属性。例如人有自己的姓名,性别,年龄,工作,喜欢的运动等信息。用JSON 来表示:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![Creative Commons License](http://i.creativecommons.org/l/by/2.5/cn/88x31.png)
本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名孙镜涛(包含链接),具体操作方式可参考此处。如您有任何疑问或者授权方面的协商,请给我留言。