JSON

XML和JSON 热门技术(2)

字号+ 作者:H5之家 来源:H5之家 2017-08-31 18:03 我要评论( )

相应地,JSON也有类似的东西,叫做JSON Schema,它本身也是JSON。比如这个例子: {title: Example Schema,type: object,properties: {firstName: {type: string},lastName: {type: string},age: {description: Age

相应地,JSON也有类似的东西,叫做JSON Schema,它本身也是JSON。比如这个例子:

{ "title": "Example Schema", "type": "object", "properties": { "firstName": { "type": "string" }, "lastName": { "type": "string" }, "age": { "description": "Age in years", "type": "integer", "minimum": 0 } }, "required": ["firstName", "lastName"] }

从XPath到JSONPath

我们经常需要一种表达式来访问整个XML中间的某个节点或者属性,XPath就是这样的表达式,比如下面这个XPath表达式就是XPath:

//*[@id="firstHeading"]/span

为什么很少提及JSON数据的path?主要原因是因为一旦有了语言原生支持,我们就不自觉地使用了,比如JavaScript中使用对象的某一个属性一样自然。但是,无论如何,一个path的表达式还是不可替代的,除了各色的开源项目,还有一个叫做JSON Pointer的规范,虽然没能得到足够多的赞成票;也有人拟了一个JSONPath的东西,这个看起来就完备得多了:

XPathAndJSONPath

还有其它的替代品吗?

有。比如YAML,表意更加丰富,还省掉了很多格式化的符号,比如JSON的引号和XML的标签记号。,记录了它和JSON以及XML之间的比较:

JSON’s foremost design goal is simplicity and universality. In contrast, YAML’s foremost design goals are human readability and support for serializing arbitrary native data structures. Thus, YAML allows for extremely readable files, but is more complex to generate and parse.

YAML is primarily a data serialization language. XML was designed to support structured documentation. XML therefore had many design constraints placed on it that YAML does not share.

看看下面的例子,不得不说,就清晰简洁为上的可读性上而言,YAML是三者中最好的:

--- receipt: Oz-Ware Purchase Invoice date: 2007-08-06 customer: given: Dorothy family: Gale items: - part_no: A4786 descrip: Water Bucket (Filled) price: 1.47 quantity: 4 - part_no: E1628 descrip: High Heeled "Ruby" Slippers price: 100.27 quantity: 1 bill-to: &id001 street: | 123 Tornado Alley Suite 16 city: East Westville state: KS ship-to: *id001 specialDelivery: > Follow the Yellow Brick Road to the Emerald City. Pay no attention to the man behind the curtain. ...

相关文章

转载请注明:热门技术 » XML和JSON

继续浏览有关

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • MySQL 5.7 新增加的 JSON 特性对应的 json 方法

    MySQL 5.7 新增加的 JSON 特性对应的 json 方法

    2017-09-01 09:03

  • Newtonsoft.Json(Json.net)的基本用法 JsonConvert.SerializeO

    Newtonsoft.Json(Json.net)的基本用法 JsonConvert.SerializeO

    2017-09-01 09:03

  • xml转化为json,多谢

    xml转化为json,多谢

    2017-08-31 16:02

  • ASP 处理JSON数据的实现代码

    ASP 处理JSON数据的实现代码

    2017-08-31 11:04

网友点评
x