2017-01-03 9 views
0

Json出力を正しい形式で表示するように、ファイルのフォーマットに関する質問があります。Groovyを使用してファイル内のjsonをフォーマットする方法

現時点では、私は以下の持っているコードは、ファイルにJSONをインポートしますが、私は、ファイルを開いたとき、それはそうのような単一の行(ワードチェックを外すラップ)でJSONを表示します。

{"products":[{"type":null,"information":{"description":"Hotel Parque La Paz (One Bedroom apartment) (Half Board) [23/05/2017 00:00:00] 7 nights","items":{"provider Company":"Juniper","provider Hotel ID":"245","provider Hotel Room ID":"200"}},"costGroups":[{"name":null,"costLines":[{"name":"Hotel Cost","search":null,"quote":234.43,"quotePerAdult":null,"quotePerChild":null} 

私が欲しいですそうのようなフォーマットの実際のJSONのように見えるようにファイルにJSONをフォーマットする:

{ 
    "products": [ 
    { 
     "type": null, 
     "information": { 
     "description": "Hotel Parque La Paz (One Bedroom apartment) (Half Board) [23/05/2017 00:00:00] 7 nights", 
     "items": { 
      "provider Company": "Juniper", 
      "provider Hotel ID": "245", 
      "provider Hotel Room ID": "200" 
     } 
     }, 
     "costGroups": [ 
     { 
      "name": null, 
      "costLines": [ 
      { 
       "name": "Hotel Cost", 
       "search": null, 
       "quote": 234.43, 
       "quotePerAdult": null, 
       "quotePerChild": null 
      } 

実質各ヘッダは、その値を格納するための独自のラインを有しています。

これを実装してファイル内に正しいjsonフォーマットを得るには、どのような方法が最適ですか?

以下

コードです:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) 
    def dataFolder = groovyUtils.projectPath +"//Log Data//" 
    def response = testRunner.testCase.getTestStepByName("GET_Pricing{id}").getProperty("Response").getValue(); 
    def jsonFormat = (response).toString() 
    def fileName = "Logged At - D" +date+ " T" +time+ ".txt" 
    def logFile = new File(dataFolder + fileName) 


    // checks if a current log file exists if not then prints to logfile 

    if(logFile.exists()) 
    { 

    log.info("Error a file named " + fileName + "already exisits") 
    } 
     else 
    { 

    logFile.write "Date Stamp: " +date+ " " + time + "\n" + jsonFormat //response 

答えて

1

あなたがかっこいいの現代版を持っている場合は、あなたが行うことができます:

JsonOutput.prettyPrint(jsonFormat) 
+0

恐ろしい、どうもありがとうございました:) – 1990prog

関連する問題