#any->convert(from?: 'json'|yaml|'csv'|'markdown'|'latex', to?: 'json'|yaml|'csv'|html|'markdown'|'latex', header?: bool, as?: type, headers?: list, indented?: bool) → any
更改值的格式。from:读取以某种格式(json、yaml、csv、markdown 或 latex)编写的文本;to:将值写成某种格式(json、yaml、csv,或用于文档的 html、markdown 和 latex);两者同时指定即可进行转换。读取 csv:没有表头行时使用
not header。读取 json、yaml 或 csv:as:指定类型后,会校验数据并将其转换为记录。写入 csv:headers:选择并排列列。写入 json:indented 会将其分行展开。->from(type:) 和 ->to(type:) 是 from: 与 to: 单独使用时的简写。| 参数 | 接受 |
|---|---|
from 可选 | 以下单词之一:'json'|yaml|'csv'|'markdown'|'latex' |
to 可选 | 以下单词之一:'json'|yaml|'csv'|html|'markdown'|'latex' |
header 可选 | 一个标志:header 或 not header |
as 可选 | type |
headers 可选 | list |
indented 可选 | 一个标志:indented 或 not indented |
参数可以命名:->convert(…, to: …, header: …)。
示例
<!-- 'apple': the json text is read into a list of maps -->
<echo>{ '[{"fruit": "apple"}]'->convert(from: json)->first().fruit }</echo>
<let name="prices" value="[{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]" />
<!-- fruit,price on the first line, then one line a row: the maps' keys name the columns -->
<echo>{ $prices->convert(to: csv) }</echo>
<!-- 'apple: 3': yaml read, then written as json -->
<echo>{ 'apple: 3'->convert(from: yaml, to: json) }</echo>// 'apple': the json text is read into a list of maps
echo '[{"fruit": "apple"}]'->convert(from: json)->first().fruit
$prices = [{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]
// fruit,price on the first line, then one line a row: the maps' keys name the columns
echo $prices->convert(to: csv)
// 'apple: 3': yaml read, then written as json
echo 'apple: 3'->convert(from: yaml, to: json)