java学习---groovy对json数据的处理

下载安装groovy:
https://groovy.jfrog.io/artifactory/dist-release-local/groovy-zips/apache-groovy-binary-4.0.2.zip
下载之后直接解压就可以使用,将bin目录添加到系统环境变量中
在IDE中使用:
在这里插入图片描述

import groovy.json.JsonSlurper

class demo {

    static def execSQL(reuldid, sourceId, envId) {
        String url = "http://godata.test.netease.com//open/api/run/rule/" + reuldid + "?sourceId=" + sourceId + "&envId=" + envId + "&token=c4a90bd4-0d42-4412-a166-fdf4ec8f1951"
        // 发送请求获取返回信息
        def conn = new URL(url).openConnection()
        // 设置请求的方式
        conn.requestMethod = "GET"
        // 创建JsonSlurper对象
        def json = new JsonSlurper()
        // 将响应信息序列化成json
        def response = json.parseText(conn.content.text)
        //提取json中的value
        def data = response.data
        if (response.code == 200 && data != '') {
            return data
        } else {
            return null
        }
    }
	// 对比数据库与api响应数据
    static def contrastresult(dbresponse, apiresponse) {
        if (dbresponse != null) {
            if (dbresponse.size() == apiresponse.size()) {
                def obj = dbresponse[0]
                // 将数组中的对象拿出来转成map
                obj instanceof Map
                // 获取map中的所有key
                def key_array = obj.keySet()
                for (int i = 0; i < dbresponse.size(); i++) {
                    for (int j = 0; j < key_array.size(); j++) {
                        String db_value = dbresponse[i].get(key_array[j])
                        String api_value = apiresponse[i].get(key_array[j])
                        if (db_value != api_value) {
                            return "false"
                        }
                    }
                }
                return 'true'
            } else {
                return 'false'
            }
        } else {
            return '数据库查询失败'
        }
    }

    static void main(String[] args) {
        String reuldid = '7FFA'
        String sourceId = '3'
        String envId = '1'
        def db_data = execSQL(reuldid, sourceId, envId)
        def conn = new URL("https://xtpadmin-autotest.zts.com.cn/api/v1/org?userId=8160&token=69a2226713e8b4c4717cf01f66c78974&page=1&orgid=&orgname=").openConnection()
        conn.requestMethod = "GET"
        def json = new JsonSlurper()
        def response_api = json.parseText(conn.content.text)
        def api_data = response_api.data.list.data
        String retult = contrastresult(db_data, api_data)
        println retult
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值