2017-05-06 1 views
0

私は、Ready!Api 1.9.0のGroovyスクリプトを使用して、SOAP応答で返されるbase64文字列をデコードし、結果のJSONオブジェクトをjsonファイルに格納します。その結果のファイルを取得し、JsonSlurperと解析してMapオブジェクトを取得します。JsonSlurper.parse(JSONFile)から返されたMapオブジェクトを反復処理するにはどうすればよいですか?

このオブジェクトは、キーを見つけてその値をアサートできるように反復処理する必要があります。なぜキーが見つからないのか分かりません。私がmap.get(key)を使ってキーを直接呼び出すと、 "No such property"というエラーが出ます。 map.get( 'key')を使って直接呼び出すとnullを返します。また、私はMap.each{k -> log.info("${k}")}を試してみましたが、 'interface.java.util.Map'を返し、期待されるキーのリストは返しません。解析の前​​に、ではない完全なJSONしかし、JSONの

//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}")} 

... //サンプル:

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

あなたはJSONのサンプルを共有することができますか? – Raphael

+0

@Raphael元の投稿を修正しました。 – justAguy88

答えて

1

質問のタイトルは非常に明確であるので、ですのでお答えしましょう。コンテンツ{"foo":42,"bar":true}でファイルx.jsonを考えると

は、次のスニペットは、ファイルを読み込み、すべてのキーと値のペアを出力します。

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

結果(興味深いちらほら:キーが並べ替えられますが、それは問題ではありません):

bar : true 
foo : 42 

あなたの質問の本体はかなり混乱しています。最初はすべて無関係に見えるので、上記のスニペットが助けになるかどうかはわかりませんが、それが実現することを願っています。

は今interface java.util.Mapであなたの奇妙な結果について:

Map.each { println it }利回りinterface java.util.Map、これはあなたのマップオブジェクトをJSONファイルを読んでから生じていない、オブジェクトMap.classに「反復」され、完全に正常です。

はこれを説明する別の方法:

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 
! 
+0

あまりにも@Hugues Moreau、私の問題を完全に解決していただきありがとうございます。私は初心者の間違いだと思った。 – justAguy88

関連する問題