在 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?

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

问题描述

这个问题仍然相关!

在我的任务中,json 进入了我的输入,这是我事先不知道的.我需要将所有 json 字段类型收集到类型"中并使用 reader.outputLines 返回所有值.现在 json 字段类型的列表是这样形成的:def types = list.find().值()*.获取类()*.简单名称但是当第一个 json 块中的相同字段为空时,我面临一个问题,而在第二个中整数和类型被定义为空,而不是整数.

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天全站免登陆