2

I am trying to get all the values of json string as a single string. for example in xquery xml

let $x := <a> welcome to the world of <b> JSONiq </b></a>
return string($x)

will return welcome to the world of JSONiq

what is the equvalent result of of following documents in JSONiq:

let $y := {"a":"welcome to the world of ","b":" JSONiq"}
return xxxx($y)

The result should be same welcome to the world of JSONiq If you know in javascript also it would be great.

4

2 回答 2

1

首先,您需要获取所有值,无论是 withlibjn: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”,因为对象键是无序的

于 2014-01-13T21:22:52.037 回答
1

在此处查找递归 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

赫尔曼。

于 2015-12-13T12:42:00.650 回答