Groovy中json的一些操作

2022-07-08 21:12:10 字數 1316 閱讀 9118

在測試過程中,尤其是介面自動化測試框架搭建時,經常會碰到 json 檔案或者資料流的處理,下面看看json的一些處理例項:

/**

* @author

:zackzhou

* @date :created in 2020/9/7 5:49 pm

* @description :groovy json 的一些操作

* @modified by:

* @version:

*/import

groovy.json.jsonbuilder

import

groovy.json.jsonoutput

import

groovy.json.jsonslurper

def dids = ["1","2"]

def gids =["1","2"]

def did_content =

def gid_content =

for(did in dids)

") single.put("deviceseri","")

single.put("devicetype","")

did_content.add(single)

}for

(gid in gids)

") single.put("operator","2")

single.put("classid","0")

single.put("targetid","0")

gid_content.add(single)

}def did_json = new file("./did.json")

def gid_json = new file("./gid.json")

did_json.withwriter

gid_json.withwriter

)}//

解析json檔案

jsonslurper jsonslurper = new

jsonslurper()

def did_list =jsonslurper.parse(did_json)

assert did_list instanceof

list

(list)did_list.each }//

將物件轉成json 方法2

jsonbuilder jsonbuilder = new

jsonbuilder()

jsonbuilder.content =did_list

println jsonbuilder.toprettystring()

groovy中的一些小技巧

對時間格式的操作 date date date.parse dd mm yyyy 31 12 2010 string out date.format mm dd yyyy println out 將會輸出 12 31 2010 獲得每個月的最後一天 calendar cc gregoriancale...

關於CollectionView的一些操作記錄

通過重寫uicollectionviewflowlayout中的layoutattributesforelementsinrect方法可以讓uicollectionview顯示諸如瀑布流 水平流動布局等各種樣式的布局。其原理就是layoutattributesforelementsinrect方法本...

docker的一些實操筆記

2.啟動nginx docker run name nginx p 80 80 d nginx 這樣就簡單的把nginx啟動了,但是我們想要改變配置檔案nginx.conf 進入容器,命令 docker exec it nginx bash nginx.conf配置檔案在 etc nginx 下面,...