$util.xml 中的 XML 帮助程序 - Amazon AppSync
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$util.xml 中的 XML 帮助程序

注意

我们现在主要支持 APPSYNC_JS 运行时系统及其文档。请考虑使用 APPSYNC_JS 运行时系统和此处的指南。

$util.xml 包含一些帮助程序方法,可以更轻松地将 XML 响应转换为 JSON 或字典。

$util.xml 实用程序列表

$util.xml.toMap(String) : Map

将 XML 字符串转换为字典。

Input: <?xml version="1.0" encoding="UTF-8"?> <posts> <post> <id>1</id> <title>Getting started with GraphQL</title> </post> </posts> Output (JSON representation): { "posts":{ "post":{ "id":1, "title":"Getting started with GraphQL" } } } Input: <?xml version="1.0" encoding="UTF-8"?> <posts> <post> <id>1</id> <title>Getting started with GraphQL</title> </post> <post> <id>2</id> <title>Getting started with Amazon AppSync</title> </post> </posts> Output (JSON representation): { "posts":{ "post":[ { "id":1, "title":"Getting started with GraphQL" }, { "id":2, "title":"Getting started with Amazon AppSync" } ] } }
$util.xml.toJsonString(String) : String

将 XML 字符串转换为 JSON 字符串。这与 toMap 类似,只不过输出是字符串。如果您要直接转换 XML 响应并将其从 HTTP 对象返回到 JSON,这非常有用。

$util.xml.toJsonString(String, Boolean) : String

使用可选的布尔值参数将 XML 字符串转换为 JSON 字符串,以确定您是否要对 JSON 进行字符串编码。