Why to use JSON?

来源:互联网 发布:一带一路 就业 知乎 编辑:程序博客网 时间:2024/04/29 20:08

Why to use JSON?

Accroading to www.json.org, you can know that It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. So we should use json instead of xml in javascript. As we know, javascript is a scripting language, so it need speed many resource to parse xml string. If we want to parse xml string, We need create DOMParser object first, then use the object to parse the xml string. It is very hard to master. However, we can easy to do this with JSON.

For example:
XML:
<cmd op="CreateObject">
 <ID>007</ID>
 <Name>new object</Name>
 <Attribute>
  <CreateTime>2007-8-31</CreateTime>
  <ModifyTime>2007-8-31</ModifyTime>
 </Attribute>
</cmd>

JSON:
{cmd:{op:CreateObject,ID:007,Name:new object,Attribute:{CreateTime:2007-8-31, ModifyTime:2007-8-31}}}

How to get the object name with xml?

var parser = new DOMParser();
var doc = parser.ParserFromString(xmlstring, "text/xml");
var objectName = doc.documentElement.getElementsByTagName("Name").item(0).nodeValue;

However, It's very easy to get the object name with json.

var objectname = jsonString.cmd.Name;

 

Which way you like?

原创粉丝点击