英文:
Groovy: How to fetch the particular list of values from json output
问题
"Expected result :"
"value" : "-130.0 -> -120.0: 129 | -120.0 -> -110.0: 82 | -110.0 -> -100.0: 20 | -100.0 -> -90.0: 1 | -90.0 -> -80.0: 0 | -80.0 -> -70.0: 0 | -70.0 -> -60.0: 0 | -60.0 -> -50.0: 0"
英文:
I am trying to get a list of values from the Json output.
MY CODE :
def VALUES_BEFORE = sh """
curl -X POST "http://node-01.xyz.com:32010/abc/def" \\
-H "accept: application/json" \\
-H "Content-Type: application/json" \\
-d '{ "cell_ids": [${payload}] }' | json_pp
"""
def json = readJSON text: VALUES_BEFORE
def mylist = json .value
echo "Values are ${mylist}"
Output is below JSON :
[
{
"def" : "bins",
"value" : 294
},
{
"def" : "valid_bins",
"value" : 294
},
{
"def" : "covered_bins",
"value" : 1
},
{
"def" : "sum",
"value" : 415
},
{
"def" : "histro",
"value" : "-130.0 -> -120.0: 129 | -120.0 -> -110.0: 82 | -110.0 -> -100.0: 20 | -100.0 -> -90.0: 1 | -90.0 -> -80.0: 0 | -80.0 -> -70.0: 0 | -70.0 -> -60.0: 0 | -60.0 -> -50.0: 0"
}
]
Expected result :
"value" : "-130.0 -> -120.0: 129 | -120.0 -> -110.0: 82 | -110.0 -> -100.0: 20 | -100.0 -> -90.0: 1 | -90.0 -> -80.0: 0 | -80.0 -> -70.0: 0 | -70.0 -> -60.0: 0 | -60.0 -> -50.0: 0"
Your help would be helpful.
Thanks
答案1
得分: 0
变体 1
def mylist = json.find { it.def == "histro" }.value
变体 2
def mylist2 = json.collect { [(it.def):it.value] }.histro
这将将 JSON 输出转换为一个以 def
为键,value
为值的映射。然后,要访问任何条目,请直接使用该映射。
英文:
Here some more simpler approaches of the previous answer:
Variant 1
def mylist = json.find { it.def == "histro" }.value
Variant 2
def mylist2 = json.collect { [(it.def):it.value] }.histro
This converts the json output to a map having def
as the key and value
as the value. Then to access any entry use the map directly.
答案2
得分: 0
以下是翻译好的内容:
import groovy.json.*
def json = new JsonSlurper().parseText('''
[
{
"def" : "bins",
"value" : 294
},
{
"def" : "valid_bins",
"value" : 294
},
{
"def" : "covered_bins",
"value" : 1
},
{
"def" : "sum",
"value" : 415
},
{
"def" : "histro",
"value" : "-130.0 -> -120.0: 129 | -120.0 -> -110.0: 82 | -110.0 -> -100.0: 20 | -100.0 -> -90.0: 1 | -90.0 -> -80.0: 0 | -80.0 -> -70.0: 0 | -70.0 -> -60.0: 0 | -60.0 -> -50.0: 0"
}
]''')
String result = json.findResult{ 'histro' == it.def ? it.value : null }
assert result == '-130.0 -> -120.0: 129 | -120.0 -> -110.0: 82 | -110.0 -> -100.0: 20 | -100.0 -> -90.0: 1 | -90.0 -> -80.0: 0 | -80.0 -> -70.0: 0 | -70.0 -> -60.0: 0 | -60.0 -> -50.0: 0'
英文:
Essentially a straight-forward one-liner, using findResult
:
import groovy.json.*
def json = new JsonSlurper().parseText '''\
[
{
"def" : "bins",
"value" : 294
},
{
"def" : "valid_bins",
"value" : 294
},
{
"def" : "covered_bins",
"value" : 1
},
{
"def" : "sum",
"value" : 415
},
{
"def" : "histro",
"value" : "-130.0 -> -120.0: 129 | -120.0 -> -110.0: 82 | -110.0 -> -100.0: 20 | -100.0 -> -90.0: 1 | -90.0 -> -80.0: 0 | -80.0 -> -70.0: 0 | -70.0 -> -60.0: 0 | -60.0 -> -50.0: 0"
}
]'''
String result = json.findResult{ 'histro' == it.def ? it.value : null }
assert result == '-130.0 -> -120.0: 129 | -120.0 -> -110.0: 82 | -110.0 -> -100.0: 20 | -100.0 -> -90.0: 1 | -90.0 -> -80.0: 0 | -80.0 -> -70.0: 0 | -70.0 -> -60.0: 0 | -60.0 -> -50.0: 0'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论