如何在JSON中表达XML标签属性? [英] How would I express XML tag attributes in JSON?

查看:149
本文介绍了如何在JSON中表达XML标签属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Web应用程序设计API.

I am designing an API for my webapp.

我正在考虑仅支持JSON响应(不支持XML),因为它更加简化.

I was thinking to support only JSON responses (not XML) because more streamlined.

但是我刚刚碰到了这个XML:

But I have just bumped to this XML:

<folders>
    <folder id="123" private="0" archived="0" order="1">Shopping</folder>
</folders>

,我想知道对应的JSON会如何.我有一种感觉,在这种情况下,XML会更紧凑.

and I was wondering how the corresponding JSON would be. I have the feeling, in this case, XML would be more compact.

推荐答案

也许:

{
  "folders": [
    { "id":123, "private":0, "archived":0, "order":1, "title":"Shopping" },
    ...
  ]
}

由于XML和JSON之间没有确切的对应关系,因此您可以自由地(例如,必须定义)这两个数据结构的映射方式.例如,在上文中,文件夹"元素隐含在文件夹"数组中的嵌套对象中.

Because there is not an exact correspondence between XML and JSON, you are free (e.g. have to define) how the two data-structures map. For instance, in the above, the "folder" element is implicit in the nested objects in the "folders" array.

可以将其扩展为:

"folders": [{"folder": { .... }]

等等,但仍然存在无法像XML一样捕获内容+属性的问题.无论如何,您的数据结构-> JSON | XML序列化程序都可能以特定的方式工作(并且请使用库,而不是手动滚动"的JSON-string-munging ).那是; XML和JSON的格式应该(以某种方式)由用于传输的数据结构统一规定.

Etc, but there is still the problem of not being able to capture content+attributes as consistently as XML. In any case, your data-structure -> JSON|XML serializer likely works in a particular way (and please, please, use a library, not "hand-rolled" JSON-string-munging). That is; the format of the XML and JSON should be uniformly dictated (somehow) by the data-structure for transmission.

这篇关于如何在JSON中表达XML标签属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆