如何在Groovy中将String转换为List

时间:2014-12-15 13:51:41

标签: groovy titan gremlin

我在转换字符串时出现问题" [3]"这里:

  p = {"results":["[3]","[3]"],"success":true,"version":"2.4.0","queryTime":63.102287}

到列表。

p [0]给了我:

 {"results":["[3]"],"success":true,"version":"2.4.0","queryTime":68.24303}

所以我以为我可以使用Eval.me(p [0])但是它给了我一个错误说:

  {"message":"","error":"javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: static groovy.util.Eval.me() is applicable for argument types: (com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline) values: [[GremlinStartPipe, GraphQueryPipe(has,vertex), IdentityPipe, PropertyPipe(Sannolikhet), RangeFilterPipe(0,0)]]\nPossible solutions: me(java.lang.String), me(java.lang.String, java.lang.Object, java.lang.String), is(java.lang.Object), use([Ljava.lang.Object;), _(groovy.lang.Closure), dump()","api":{"description":"evaluate an ad-hoc Gremlin script for a graph.","parameters":{"rexster.returnKeys":"an array of element property keys to return (default is to return all element properties)","rexster.showTypes":"displays the properties of the elements with their native data type (default is false)","load":"a list of 'stored procedures' to execute prior to the 'script' (if 'script' is not specified then the last script in this argument will return the values","rexster.offset.end":"end index for a paged set of data to be returned","rexster.offset.start":"start index for a paged set of data to be returned","params":"a map of parameters to bind to the script engine","language":"the gremlin language flavor to use (default to groovy)","script":"the Gremlin script to be evaluated"}},"success":false} 

任何知道如何在不使用Eval.me的情况下完成此操作的人吗?

修改

澄清,我的剧本的第一部分:

 p = g.V('containerName','CvsRisk').outE.inV.hasNot('Sannolikhet',null).'Sannolikhet';

我"提取"具有containerName CvsRisk的容器中的所有顶点,其具有名为" Sannolikhet"的属性。那不是空的。 运行脚本的这一部分会从服务器给出以下响应:

  {"results":["[3]","[3,3]","[3,3]","[3,3]","[3,3]","[3,3]","[3,3]","[3]","[3,3]","[3]","[3,3]","[3,3]"],"success":true,"version":"2.4.0","queryTime":20.429118}

这就是我想要的,因为我的所有物业都命名为" Sannolikhet"包含一系列值。

从这里我想迭代结果并总结字符串中的值,但为了做到这一点,我需要将字符串转换为列表。

2 个答案:

答案 0 :(得分:0)

您可以使用JsonSlurper解析生成的JSON,并收集results并应用Eval。如果你确定没有方法准备好在gremlin中获取值,那么我想你必须使用这样的东西:

更新:第二个想法,p.results应该可以正常工作,忘记前两个注释行:

//result='{"results":["[3]","[3,3]","[3,3]","[3,3]","[3,3]","[3,3]","[3,3]","[3]","[3,3]","[3]","[3,3]","[3,3]"],"success":true,"version":"2.4.0","queryTime":20.429118}'

//json = new groovy.json.JsonSlurper().parseText result

p = ["results":["[3]","[3,3]","[3,3]","[3,3]","[3,3]","[3,3]","[3,3]","[3]","[3,3]","[3]","[3,3]","[3,3]"]]

assert p.results.size() == 12

vertices = p.results.collect { Eval.me(it) }

assert vertices.size() == 12

assert vertices[0] == [3]
assert vertices[1] == [3, 3]
assert vertices[-3] == [3]
assert vertices[-1] == [3, 3]

答案 1 :(得分:0)

我找到了解决方案

整个解决方案是:

  def p = g.V('containerName','CvsRisk').outE.inV.hasNot('Sannolikhet',null).'Sannolikhet';
  m= [];for(i in p){m.add(Eval.me(i));};k=m.sum();k*.toInteger().sum()