问题描述
在我们的自动化框架中,我们正在处理JSON文件中的测试数据。 下面是示例文件
{
"Sheet1": [
{
"id": "1","firstname": "test","lastname": "last","email": "[email protected]","gender": "female","ip_address": "26.58.193.2"
},{
"id": "2","firstname": "test2","lastname": "last2","email": "[email protected]","gender": "male","ip_address": "229.179.4.212"
}
]
}
首先,我们将JSON文件转换为MapObject,然后从地图中读取数据。
def getobjectkeyvalueInTheMapList(fileName,objectName,key) {
def keyvalue = []
Map jsonMap = new LinkedHashMap()
jsonMap = converjsonToMapObject(fileName)
def getKey = jsonMap.get(objectName)
}
getKey返回
[[email:[email protected],firstname:test,gender:female,id:1,ip_address:26.58.193.2,lastname:last],[email:[email protected],firstname:test2,gender:male,id:2,ip_address:229.179.4.212,lastname:last2]]
从上面的地图中,我如何获得email
的{{1}}键值
即,如果id =1
,那么我想阅读id = 1
解决方法
签出this。在Groovy中,您可以定义这样的地图def jsonMap = [:]
。您可以通过多种方式获取值,例如其中一些:jsonMap.email
或jsonMap.['email']
将返回[email protected]
。
您有一个地图列表,目标是找到带有
id==1
。如果您必须经常这样做,那么最好的方法是
通过id
来更好地塑造数据以访问它们。
例如通过id
用类似的方式构建用户地图
def usersById = data.collectEntries{ [it.id,it] }
然后usersById[1]
将为您提供带有id==1
的地图。