Найти значение в HTTP-ответе JSON - PullRequest
0 голосов
/ 10 октября 2018

Я хочу получить значение city из приведенного ниже httpResponse (JSON Response), где фрукты - это Apple.Я не могу найти способ добавить это условие в мой отличный сценарий.

{
    "userInformation": {
        "Name": "John",
        "Location": "India"
    },
    "details": [
        {
            "fruit": "Apple",
            "color": "Red",
            "city": "New Delhi",
            "luckyNumber": 10
        },
        {
            "fruit": "Banana",
            "color": "yellow",
            "city": "Goa",
            "luckyNumber": 12
         }
     ]
}

1 Ответ

0 голосов
/ 10 октября 2018

JsonSlurper - это то, что вам нужно.Он анализирует объект JSON до простого значения Map, по которому можно легко перемещаться, чтобы найти нужное значение.

import groovy.json.JsonSlurper

def input = '''{
    "userInformation": {
        "Name": "John",
        "Location": "India"
    },
    "details": [
        {
            "fruit": "Apple",
            "color": "Red",
            "city": "New Delhi",
            "luckyNumber": 10
        },
        {
            "fruit": "Banana",
            "color": "yellow",
            "city": "Goa",
            "luckyNumber": 12
         }
   ]
}
'''

def slurped = new JsonSlurper().parseText(input)

slurped.details.find { it.fruit == 'Apple' }?.city
...