栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985511.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号