Groovy:如何从JSON输出中提取特定的值列表

huangapple go评论77阅读模式
英文:

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'     

huangapple
  • 本文由 发表于 2023年6月12日 19:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定