首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从jsoniq或javascript中的json文档中获取所有字符串?

如何从jsoniq或javascript中的json文档中获取所有字符串?
EN

Stack Overflow用户
提问于 2014-01-12 19:41:48
回答 2查看 816关注 0票数 2

我试图将json字符串的所有值作为单个字符串。例如,在xquery中

代码语言:javascript
复制
let $x := <a> welcome to the world of <b> JSONiq </b></a>
return string($x)

将返回welcome to the world of JSONiq

JSONiq中下列文件的等价物是什么?

代码语言:javascript
复制
let $y := {"a":"welcome to the world of ","b":" JSONiq"}
return xxxx($y)

结果应该是相同的welcome to the world of JSONiq,如果您知道在javascript,也将是很好的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-13 21:22:52

首先,您需要获得所有的值,无论是使用libjn:values还是它的定义,然后可以使用fn:string-join获得一个字符串:

所以

代码语言:javascript
复制
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(), "")

代码语言:javascript
复制
let $y := {"a":"welcome to the world of ","b":" JSONiq"}
return string-join($y() ! string($y(.)), "")

这还可能返回"JSONiqwelcome to the world of ",因为对象键是无序的。

票数 1
EN

Stack Overflow用户

发布于 2015-12-13 12:42:00

找到用于尝试的递归JSONiq脚本这里。只需执行类型转换和递归调用即可:

代码语言:javascript
复制
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), "@")

"@“仅用于显示边界的位置,可替换为"":

代码语言:javascript
复制
welcome to the world of@JSONiq@m

赫尔曼。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21079385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档