我试图将json字符串的所有值作为单个字符串。例如,在xquery中
let $x := <a> welcome to the world of <b> JSONiq </b></a>
return string($x)将返回welcome to the world of JSONiq
JSONiq中下列文件的等价物是什么?
let $y := {"a":"welcome to the world of ","b":" JSONiq"}
return xxxx($y)结果应该是相同的welcome to the world of JSONiq,如果您知道在javascript,也将是很好的。
发布于 2014-01-13 21:22:52
首先,您需要获得所有的值,无论是使用libjn:values还是它的定义,然后可以使用fn:string-join获得一个字符串:
所以
declare namespace libjn = "http://jsoniq.org/function-library";
let $y := {"a":"welcome to the world of ","b":" JSONiq"}
return string-join(libjn:values($y) ! string(), "")或
let $y := {"a":"welcome to the world of ","b":" JSONiq"}
return string-join($y() ! string($y(.)), "")这还可能返回"JSONiqwelcome to the world of ",因为对象键是无序的。
发布于 2015-12-13 12:42:00
找到用于尝试的递归JSONiq脚本这里。只需执行类型转换和递归调用即可:
declare function local:strings($v as item()) as xs:string*
{
typeswitch ($v)
case $object as object() return
for $k in jn:keys($object)
return local:strings($object($k))
case $array as array() return
for $member in jn:members($array)
return local:strings($member)
default return $v
};
let $y := {"a":"welcome to the world of", "b":" JSONiq", "x":{"d":"m"}}
return string-join(local:strings($y), "@")"@“仅用于显示边界的位置,可替换为"":
welcome to the world of@JSONiq@m赫尔曼。
https://stackoverflow.com/questions/21079385
复制相似问题