XML在美元的幫手 - AWS AppSync

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

XML在美元的幫手

注意

我們現在主要支援 APPSYNC _JS 執行階段及其文件。請考慮在此處使用 APPSYNC _JS 運行時及其指南

$util.xml包含輔助方法,可以更容易地將XML響應翻譯為JSON或字典。

$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 AWS AppSync</title> </post> </posts> Output (JSON representation): { "posts":{ "post":[ { "id":1, "title":"Getting started with GraphQL" }, { "id":2, "title":"Getting started with AWS AppSync" } ] } }
$util.xml.toJsonString(String) : String

將XML字串轉換為JSON字串。這類似於 toMap,不同之處在於輸出是一個字符串。如果您想直接將HTTP對象的響應轉換並返XML回到,這非常有用JSON。

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

將XML字串轉換為具有選擇性布林參數的JSON字串,以決定是否要對字串進行編碼。JSON