简体   繁体   中英

Unable to filter object from json array in groovy

In groovy, I have below object. (Type: [Ljava.lang.Object)

 test = [  
   {  
      "id":"rod_1565173117796",
      "userName":"rod",
      "displayName":"Rod",
      "date":1565173117796,
      "comment":"ok"
   },
   {  
      "id":"rod_1565173139923",
      "userName":"rod",
      "displayName":"Rod",
      "date":1565173139923,
      "comment":"fine"
   }
]

I want to modify / delete this list of JSON array based on id.

I tried below thing filter the required json object from list.

parsedJSON = parser.parseText(test);  
parsedJSON.findAll{ it.id == 'rod_1565173139923' });

Which is giving me that

No such property: id for class: java.lang.String

What wrong i am doing?

Thanks!

just several syntax fixes and your code works:

def test = '''[  
   {  
      "id":"rod_1565173117796",
      "userName":"rod",
      "displayName":"Rod",
      "date":1565173117796,
      "comment":"ok"
   },
   {  
      "id":"rod_1565173139923",
      "userName":"rod",
      "displayName":"Rod",
      "date":1565173139923,
      "comment":"fine"
   }
]'''

def parser = new groovy.json.JsonSlurper()
def parsedJSON = parser.parseText(test);  
def filtered = parsedJSON.findAll{ it.id == 'rod_1565173139923' }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM