How in groovy (java) automatically find out the json field type and return values from json, replacing null with empty space?

筅森魡賤 提交于 2021-01-05 09:13:43

问题


The question is still relevant!

In my task, json comes to my input, which I do not know in advance. I need to collect all json field types into "types" and return all values ​​using reader.outputLines. Now the list of json field types is formed like this: def types = list.find (). Values ​​() *. GetClass () *. SimpleName But I am faced with a problem when the same field in the first json block is null, and in the second the integer and the type is defined as null, and not as Integer.

  1. How to make sure that the type is determined by going through all the json blocks for each field, and not making an output based on the first block, and if everything is null or "" (empty), assign the default String?
  2. When returning values ​​from json using reader.outputLines, replace null with "" (empty)?
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)
    }
}

回答1:


I'd suggest the following strategy: Iterate as long over the rows until you know each type or you reach some sort of end (depending on the amount of rows, you might want to just look in all rows or just give up after N rows). For each row try to determine the type and keep book of the type, but only "promote" from null to the type in your bookkeeping.

import groovy.json.JsonSlurperClassic

def data = new JsonSlurperClassic().parseText("""
[{"AUTO":"bmw",   "HOME":null, "JOB":""},
 {"AUTO":"audi",  "HOME":135,  "JOB":null},
 {"AUTO":"opel1", "HOME":10,   "JOB":null}]
""")

def types = [:]
def it = data.iterator() // or only take(10) e.g.
while (it.hasNext() && !(types && types.every{ _, v -> v })) {
    it.next().collectEntries{ k, v -> 
        [k, v?.getClass()?.simpleName] 
    }.inject(types, { m, entry ->
        if (entry.value!=null || !m.containsKey(entry.key)) {
            m[entry.key] = entry.value
        }
        return m
    })
}

// if there are types missing (values with null), replace them here with
// your default)

println types
// → [AUTO:String, JOB:String, HOME:Integer]


来源:https://stackoverflow.com/questions/64802501/how-in-groovy-java-automatically-find-out-the-json-field-type-and-return-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!