我正在为我的网络应用程序设计一个API.

我想只支持JSON响应(而不是XML),因为它更精简.

但我刚刚谈到了这个XML:

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

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

推荐答案

Perhaps:

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

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.

This could be expanded as in:

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

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相关问答推荐

Jolt转换问题—使用键查找匹配对象

如何使用ChoETL将复杂的JSON转换为CSV

如何判断响应数组是否存在以及S是否有其他内容...?

用于参考的Jolt变换

将pyspark.sql.Rowtype数据转换为Json字符串,消除Azure Databricks NB中的值

Python 将 struct 化文本转换和筛选为对象

如何使用 Google 表格应用程序脚本将 JSON 中的多个字段提取到 Google 表格中

使用 jq 和 awk 拆分大型 JSON 文件

ORA-01422: 精确提取返回的行数超过了与 json 对象组合的请求数

使用 boost 属性树读取 int 数组

.NET 对象最灵活的序列化是什么,但实现起来很简单?

在 JSON 编码的 HTML5 数据属性中转义/编码单引号

如何在 Perl 中将简单的哈希转换为 json?

在 JSON.stringify() 的输出中隐藏空值

有必要清理 JSON 吗?

Jsonpath 与 Jackson 或 Gson

如何在已声明的 JSON 对象中添加键值对

我应该如何处理 JSON 中的 HATEOAS 链接和引用?

确保数组中的项目属性在 Json Schema 中是唯一的?

jq:按属性分组和键