[英]How in groovy (java) automatically find out the json field type and return values from json, replacing null with empty space?
[英]How in my example in groovy (java) to automatically detect the json field type?
在我的任务中,我处理传入的 json 并使用 ResultSetReader 将数据作为数据集返回。 所有类型都必须写入“类型”。 它们现在是这样定义的: def types = list.find (). Values () *. GetClass () *. SimpleName
def types = list.find (). Values () *. GetClass () *. SimpleName
但是这里有两个问题:
如果在 json 的第一个块中某些字段为“null”,而在下一个块中有一个数字,则类型写为“null”,而不是“Integer”。
如果在所有json块中有人的字段是“null”,那么写“null”,你需要写,比如默认写“String”,这样程序才不会停止工作。
我应该怎么做? 恳请您不要重写我所有的代码,但要专门针对这个问题提出建议。 “类型”应仅包含格式类型 [“字符串”、“整数”、“字符串”](例如)。
无需告知类型 [NameJSON: String, NameJSON: Integer, NameJSON: Sting] 的位置,因为在这种情况下我无法使用 ResultSetReader。
import groovy.json.JsonSlurper
import ru.itrpro.xm.plugins.groovy.ResultSetReader;
class XM_PARSE_XLS {
def execute(ResultSetReader reader, String pfile) {
def jsonSlurper = new JsonSlurper()
def list = jsonSlurper.parseText pfile
List names = list.inject( new LinkedHashSet<>() ){ res, map ->
res.addAll map.keySet()
res
}.toList()
def types = list.find().values()*.getClass()*.simpleName
//formation of the dataset header
reader.outputLinesSetHeaders(names,types);
list.each{ e ->
reader.outputLines names.collect{ e[ it ] }
//println names.collect{ e[ it ] }
}
//closing dataset
reader.outputLinesEnd();
return null;
}
static void main(String... args) {
String pfile = """
[{"AUTO":"bmw",
"HOME":null,
"JOB":""},
{"AUTO":"audi",
"HOME":135,
"JOB":null},
{"AUTO":"opel1",
"HOME":10,
"JOB":null}]
"""
def SSC = new XM_PARSE_XLS()
def res = SSC.execute(new ResultSetReader(), pfile)
}
}
这里的工作示例。
考虑更换这个:
def types = list.find().values()*.getClass()*.simpleName
有了这个(编辑:更新了关于 BigDecimal/double 的评论中的问题):
// as requested in comments:
def getTypeDef(def value) {
(value instanceof BigDecimal) ? "double" : value.getClass().simpleName
}
def typeMap = [:].withDefault { key -> "String" }
list.each { map ->
map.each { key, value ->
if (value != null) {
typeMap[key] = getTypeDef(value)
}
}
}
def types = names.collect { name -> typeMap[name] }
这个想法是typeMap
将字段名映射到它的类型,并且默认为String
。 然后:
typeMap
中使用其类型如果任何项目的字段具有适当的值,则将使用该值(假设项目不能具有不同类型的值)。 如果没有项目具有该字段的值,则typeMap
将默认为String
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.