【问题标题】:How do I iterate on a Map object returned from JsonSlurper.parse(JSONFile)?如何迭代从 JsonSlurper.parse(JSONFile) 返回的 Map 对象?
【发布时间】:2017-05-06 17:38:26
【问题描述】:

我在 Ready!Api 1.9.0 中使用 Groovy 脚本来解码在 SOAP 响应中返回的 base64 字符串,并将生成的 JSON 对象存储在 json 文件中。然后获取生成的文件并使用 JsonSlurper 对其进行解析以获取 Map 对象。

这个对象需要被迭代,这样我才能找到一个键并断言它的值。我无法弄清楚为什么找不到密钥。如果我使用 map.get(key) 直接调用一个键,我会收到一个错误“没有这样的属性”。如果我直接使用 map.get('key') 调用它,它会返回 null。我也试过 Map.each{k -> log.info("${k}")} 它返回 'interface.java.util.Map' 而不是预期的键列表。

//create file path
def respFile = "C:\\Users\\me\\Documents\\Temp\\response.json"

    //set originaldata in response to var
    def response1 = context.expand( '${Method#Response#declare namespace ns4=\'com/service/path/v4\'; declare namespace ns1=\'com/other/service/path/v4\'; //ns1:RequestResponse[1]/ns1:GetAsset[1]/ns1:Asset[1]/ns4:DR[1]/ns4:Sources[1]/ns4:Source[1]/ns4:OriginalData[1]}' )

    //decode the data
    byte[] decoded = response1.decodeBase64()

    //create file using file path above if it doesnt exist
    def rf = new File(respFile)

    //write data to file NOTE will overwrite existing data
    FileOutputStream f = new FileOutputStream(respFile);
    f.write(decoded);
    f.close();

//begin second file
    import groovy.json.JsonSlurper;
    def inputFile = new File("C:\\Users\\me\\Documents\\Temp\\response.json")
    def parResp = new JsonSlurper().parse(inputFile)

    //test to find key
    Map.each{k -> log.info("${k}")}

.. //解析前的json样本,但不是完整的json:

{
 "Response": {
 "ecn": 1000386213,
 "header": {
 "msgRefNum": "bbb-ls-123" 
},
 "success": true,
 "duplicatedit": false,
 "subjectReturnCode": 1,
 "subject": [
 {
 "uu": 11264448,
 "name": {
 "name3": "WINSTON BABBLE",
 "dob": "19700422",
 "gender": "2",
 "ecCoded": "160824",
 "ecCodeSegment": "ZZ" 
},
 "acc": [
 {
 "ftp": "01",
 "Number": "AEBPJ3977L",
 "issued": "20010101",
 "mMode": "R" 
} ],
 "telephone": [
 {
 "telephoneType": "01",
 "telephoneNumber": "9952277966",
 "mMode": "R" 
} ],
 "address": [
 {
 "line1": "M\/O HEALTH AND FAMILY WELFARE",
 "sCode": "07",
 "cCode": 110009,
 "ac": "04",
 "reportedd": "160430",
 "mMode": "R",
 "mb": "lakjsdf blorb" 
},

【问题讨论】:

  • 你能分享一个json的样本吗?
  • @Raphael 我已经修改了原帖。

标签: json parsing groovy soapui jsonslurper


【解决方案1】:

题名很清楚,我们来回答一下吧。

给定文件x.json 和内容{"foo":42,"bar":true},以下 sn-p 读取文件并打印所有键值对:

def map = new groovy.json.JsonSlurper().parse(new File('x.json'))
map.each { key, value ->
  println "$key : $value"
}

结果(有趣的花絮:键已重新排序,但没关系):

bar : true
foo : 42

现在你的问题很混乱。所有的开始看起来都无关紧要,所以我不确定上面的 sn-p 是否会有所帮助,但我希望它会有所帮助。

现在关于 interface java.util.Map 的奇怪结果:

Map.each { println it } 产生 interface java.util.Map,这是完全正常的,您正在“迭代”对象 Map.class,而不是读取 json 文件产生的地图对象。

另一种方式来说明这一点:

Map.each { println it }
Integer.each { println it }
123.each { println it }
"HI!".each { println it }

结果:

interface java.util.Map
class java.lang.Integer
123
H
I
!

【讨论】:

  • 非常感谢@Hugues Moreau,这完全解决了我的问题。我认为这是一个新手错误。
猜你喜欢
  • 2019-06-28
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-23
  • 1970-01-01
相关资源
最近更新 更多