相应地,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的东西,这个看起来就完备得多了:
还有其它的替代品吗?
有。比如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
继续浏览有关