使用 altjson 库创建 JSON 数组
Create JSON array using altjson library
我正在为 Rebol http://ross-gill.com/page/JSON_and_REBOL 使用这个 JSON 库,我正在努力寻找创建 JSON 数组的正确语法。
我试过这个:
>> to-json [ labels: [ [ label: "one" ] [ label: "two" ] ] ]
== {{"labels":{}}}
我希望是这样的:
== {{"labels": [ {"label": "one"}, {"label": "two"} ]}}
>> ?? labels
labels: [make object! [
label: "one"
] make object! [
label: "two"
]]
>> to-json make object! compose/deep [ labels: [ (labels) ]]
== {{"labels":[{"label":"one"},{"label":"two"}]}}
正如@hostilefork 所建议的,使用逆运算向我们展示了它是如何工作的。
>> load-json {{"labels": [ {"label": "one"}, {"label": "two"} ]}}
== make object! [
labels: [
make object! [
label: "one"
]
make object! [
label: "two"
]
]
]
所以我们需要创建一个包含对象的对象。需要 compose/deep
来评估嵌套的 (
)
,以便创建对象。
to-json make object! compose/deep [
labels: [
(make object! [ label: "one" ])
(make object! [ label: "two" ])
]
]
除了 object!
方法之外,还有另一种语法更简单的方法:
>> to-json [
<labels> [
[ <label> "one" ]
[ <label> "two" ]
]
]
== {{"labels":[{"label":"one"},{"label":"two"}]}}
我正在为 Rebol http://ross-gill.com/page/JSON_and_REBOL 使用这个 JSON 库,我正在努力寻找创建 JSON 数组的正确语法。
我试过这个:
>> to-json [ labels: [ [ label: "one" ] [ label: "two" ] ] ]
== {{"labels":{}}}
我希望是这样的:
== {{"labels": [ {"label": "one"}, {"label": "two"} ]}}
>> ?? labels
labels: [make object! [
label: "one"
] make object! [
label: "two"
]]
>> to-json make object! compose/deep [ labels: [ (labels) ]]
== {{"labels":[{"label":"one"},{"label":"two"}]}}
正如@hostilefork 所建议的,使用逆运算向我们展示了它是如何工作的。
>> load-json {{"labels": [ {"label": "one"}, {"label": "two"} ]}}
== make object! [
labels: [
make object! [
label: "one"
]
make object! [
label: "two"
]
]
]
所以我们需要创建一个包含对象的对象。需要 compose/deep
来评估嵌套的 (
)
,以便创建对象。
to-json make object! compose/deep [
labels: [
(make object! [ label: "one" ])
(make object! [ label: "two" ])
]
]
除了 object!
方法之外,还有另一种语法更简单的方法:
>> to-json [
<labels> [
[ <label> "one" ]
[ <label> "two" ]
]
]
== {{"labels":[{"label":"one"},{"label":"two"}]}}