在groovy(java)中如何自动找到json字段类型并从json返回值,将null替换为空白? [英] How in groovy (java) automatically find out the json field type and return values from json, replacing null with empty space?

查看:342
本文介绍了在groovy(java)中如何自动找到json字段类型并从json返回值,将null替换为空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题仍然存在!

在我的任务中,json出现在我的输入中,我事先并不知道.我需要将所有json字段类型收集到类型"字段中,并使用reader.outputLines返回所有值.现在,JSON字段类型的列表如下所示: def types = list.find (). Values ​​() *. GetClass () *. SimpleName 但是当第一个json块中的相同字段为null,第二个整数和类型定义为null而不是Integer时,我面临一个问题.

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. 如何通过遍历每个字段的所有json块来确定类型,而不是根据第一个块进行输出,以及是否所有内容为null或"来确定类型. (空),指定默认字符串吗?
  2. 使用reader.outputLines从json返回值时,将null替换为``'' (空)?
  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)
    }
}

推荐答案

我建议采用以下策略:在行中进行尽可能长的迭代,直到 您知道每种类型,或者达到某种目的(取决于 行数,您可能只想查看所有行或放弃 N行之后).对于每一行,尝试确定其类型并保存 类型,但只能升级"从null到您的簿记类型.

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]

这篇关于在groovy(java)中如何自动找到json字段类型并从json返回值,将null替换为空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆