Groovy-JSONSlurper:将 JSON 映射转换为键=值对
Groovy-JSONSlurper: Transform JSON map into a key=value pair
我有一个 JSON 对象,我们假设它是平面的(没有嵌套)和一张地图。我怎样才能将它转换成单个键=值对字符串,使用 Groovy 中的 JSONSlurper 由制表符分隔?
例如这样的:
StringBuilder keyStr = new StringBuilder()
def json = new JsonSlurper().parseText(jsonString)
json.each{keyStr.append(it.key).append("=").append(it.value).append("\t")}
someMap.put(strKey, someValue)
//parse json-string to map
def json = new groovy.json.JsonSlurper().parseText('{"a":"1","B":"22"}')
//convert map to array of `key=value` strings
//and then join into one with new line delimiter
String txt = json.collect{"${it.key}=${it.value}"}.join('\n')
我有一个 JSON 对象,我们假设它是平面的(没有嵌套)和一张地图。我怎样才能将它转换成单个键=值对字符串,使用 Groovy 中的 JSONSlurper 由制表符分隔?
例如这样的:
StringBuilder keyStr = new StringBuilder()
def json = new JsonSlurper().parseText(jsonString)
json.each{keyStr.append(it.key).append("=").append(it.value).append("\t")}
someMap.put(strKey, someValue)
//parse json-string to map
def json = new groovy.json.JsonSlurper().parseText('{"a":"1","B":"22"}')
//convert map to array of `key=value` strings
//and then join into one with new line delimiter
String txt = json.collect{"${it.key}=${it.value}"}.join('\n')