使用altjson库创建JSON数组

时间:2015-08-30 03:35:19

标签: json rebol rebol3

我正在为Rebol http://ross-gill.com/page/JSON_and_REBOL使用这个JSON库,我很难找到创建JSON数组的正确语法。

我试过了:

>> to-json [ labels: [ [ label: "one" ] [ label: "two" ] ] ]
== {{"labels":{}}}

我希望有类似的东西:

== {{"labels": [ {"label": "one"}, {"label": "two"} ]}}

2 个答案:

答案 0 :(得分:2)

>> ?? labels

labels: [make object! [

        label: "one"

    ] make object! [

        label: "two"

    ]]


>> to-json make object! compose/deep [ labels: [ (labels) ]]

== {{"labels":[{"label":"one"},{"label":"two"}]}}

答案 1 :(得分:2)

正如@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"}]}}