如何迭代从JsonSlurper.parse(JSONFile)返回的Map对象? [英] How do I iterate on a Map object returned from JsonSlurper.parse(JSONFile)?

查看:81
本文介绍了如何迭代从JsonSlurper.parse(JSONFile)返回的Map对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a Groovy script in Ready!Api 1.9.0 to decode a base64 string which is returned in a SOAP response and store the resulting JSON object in a json file. Then take that resulting file and parse it with JsonSlurper to get a Map object.

此对象需要迭代,因此我可以找到一个键并声明其值.我无法弄清楚为什么找不到键.如果我使用map.get(key)直接调用一个键,则会收到错误消息"No such property".如果我直接使用map.get('key')调用它,则返回null.我也尝试过 Map.each {k->log.info("$ {k}")} 会返回"interface.java.util.Map",而不是预期的键列表.

This object needs to be iterated over so I can find a key and assert its value. I am unable to figure out why the keys aren't found. If I directly call a key using map.get(key) I get an error "No such property". If I directly call it using map.get('key') it returns null. I've also tried Map.each{k -> log.info("${k}")} which returns 'interface.java.util.Map' and not the expected list of keys.

//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:

.. //sample of the json before parse, not the full json though:

{
 "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" 
},

推荐答案

问题标题很清楚,让我们来回答.

The question title is very clear, so let's answer that.

给出内容为 {"foo":42,"bar":true} 的文件 x.json ,以下代码段将读取文件并打印所有键值对:

Given file x.json with content {"foo":42,"bar":true}, the following snippet reads the file and prints all key-value pairs:

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

结果(有趣的花样:按键已重新排序,但这无关紧要):

Result (interesting tidbit: the keys are reordered, but it should not matter):

bar : true
foo : 42

现在,您的问题很令人困惑.所有开头似乎都无关紧要,所以我不确定上面的代码片段会有所帮助,但我希望会有所帮助.

Now the body of your question is quite confusing. All the beginning looks irrelevant, so I'm not sure the snippet above will help, but I hope it will.

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

Now about your strange result with interface java.util.Map:

Map.each {println it} 产生 interface java.util.Map ,这完全正常,您正在迭代"对象 Map.类,而不是由于读取json文件而导致的地图对象.

Map.each { println it } yields interface java.util.Map, this is perfectly normal, you are "iterating" on the object Map.class, not your map object resulting from reading the json file.

另一种说明方式:

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
!

这篇关于如何迭代从JsonSlurper.parse(JSONFile)返回的Map对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆