繁体   English   中英

如何将json响应存储在数组中并对其进行排序

[英]How to store the json response in an array and sort it

我想将值关系id,relationshipType存储在一个数组中,进行排序,然后在Groovy中打印该数组。 到目前为止我有这个...

def slurper = new JsonSlurper()
def result = slurper.parseText(reponse)

{"RecipientRelationships": [
      {
      "RelationshipId": "15",
      "RelationshipType": "Self"
   },
      {
      "RelationshipId": "1",
      "RelationshipType": "Mother"
   },
      {
      "RelationshipId": "2",
      "RelationshipType": "Father"
   },

像这样?

import groovy.json.JsonSlurper

def response = '''{"RecipientRelationships": [
        {
            "RelationshipId": "15",
            "RelationshipType": "Self"
        },
        {
            "RelationshipId": "1",
            "RelationshipType": "Mother"
        },
        {
            "RelationshipId": "2",
            "RelationshipType": "Father"
        }]
}'''
JsonSlurper slurper = new JsonSlurper()
Map result = slurper.parseText(response)
result.RecipientRelationships.sort {
    it.RelationshipId as Integer
}.each {
    println it
}
     i need to use the results to compare it to the table values    
     id realtionships   
     1  father
     2  mothera
     31 sisterinlaw
      23    son
    24  daughter

      i have already printed the results of the query like this
      inside a function()
    {
     query = "select id , relationships from table"
      return result
     result= db.rows(query)
     }

     now i need to compare the results of the table  with the response 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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