gpt4 book ai didi

rjson:将 `list` 转换为与 Cypher DSL 兼容的类 JSON 字符串

转载 作者:行者123 更新时间:2023-12-04 09:26:35 25 4
gpt4 key购买 nike

让我们:

desired_output="{a:'1', b:'foo'}"
D = list(a=1, b="foo")

然后:

out = toJSON(D)
out
"{\"a\":1,\"b\":\"foo\"}"

identical(out, desired_output) # FALSE

是否有更好的函数 f(gsub 除外)使这个成立?

identical( f(toJSON(D)), desired_output) == TRUE

使用 cat 只是打印到屏幕上:

cat(toJSON(D))
{"a":1,"b":"foo"}

背景:

字符串的 desired_output 格式是使用 RNeo4j 包动态构建 cypher/Neo4j 图形数据库查询所必需的,例如:

# match node n with properties a=1 and b="foo"
RNeo4j::cypher(graph, query="MATCH (n{a:'1', b:'foo'}) RETURN n")

最佳答案

这适用于您的示例,希望更一般的情况:

gsub("',", "', ",                             # adds spaces after commas
gsub('"', "'", # replaces " with '
gsub('"([^"]+)":', "\\1:", # removes " around key names
toJSON(rapply(D, as.character))))) # puts " around numerics
# [1] "{a:'1', b:'foo'}"

关于rjson:将 `list` 转换为与 Cypher DSL 兼容的类 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26178862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com