如何列出 json 字段名称和值?
How to list json field names and values?
请帮我解决问题。我在 groovy 中编程(您可以使用 java 示例,它看起来像那里)。 Json来到input,不知道有多少字段。可以有 5 个字段,可能是 10 个,也可能是 50 个。我的任务是处理此 json 和 return 返回的数据,使用:
// Names of dataset columns
def names = ["a", "b", "c"];
// types of return values in each column (for any field (column) json is always String)
def types = ["String", "String", "String"];
// formation of the dataset header
reader.outputLinesSetHeaders (names, types);
// Passing the data itself from json
reader.outputLines ([it.a, it.b, it.c])
// Close the dataset
reader.outputLinesEnd ();
如果我知道传入的json,那么我会提前设置所需的字段名称,“String”的数量,并return通过引用特定的[=]编辑它们的值25=] 字段。下面的示例显示了 3 json 个字段:auto、home、job。因此,每个字段的 3 倍“String”,并将特定字段引用到 return it.auto、it.home、it.job 的值。但是,如果我不知道传入的 json?
,我怎么能做同样的事情呢?
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)
//The names of the columns of the dataset (now set statically to show an example; but in my case I don't know json and can't write it this way in advance)
def names = ["AUTO", "HOME", "JOB"];
//return types in each column (for any json, only "String" types)
def types = ["String", "String", "String"];
//формирование заголовка датасета
reader.outputLinesSetHeaders(names,types);
list.each {
//pass the values as a dataset from json (now set statically to show an example; but in my case I don't know json and can't write it this way in advance)
reader.outputLines([it?.AUTO, it?.HOME, it?.JOB]);
}
//closing dataset
reader.outputLinesEnd();
return null;
}
static void main(String... args) {
String pfile = """
[{"AUTO":"bmw",
"HOME":"vest",
"JOB":"bbds"},
{"AUTO":"audi",
"HOME":"dest",
"JOB":"aads"},
{"AUTO":"opel",
"HOME":"lest",
"JOB":"ffds"}]
"""
def SSC = new XM_PARSE_XLS()
def res = SSC.execute(new ResultSetReader(), pfile)
}
}
也许值得将传入的json的所有字段名称收集到一个列表中并指定一个“String”列表(任何传入的json的所有字段都只有“String”)与字段数相同?但是如何做到这一点以及我应该如何传递字段值(it.***)?
假设您只想知道键和值的类型,您
可以创建所有 keys/types 的联合。这假设值类型
因为一个键在所有键上都是相同的。
import groovy.json.JsonSlurper
def data = new JsonSlurper().parseText("""[{"a": 1, "b": 2, "c": 3, "x": true}, {"a": 4, "b": 5, "c": 6, "d": "Hello"}]""")
def content = data.collectEntries{
it.collectEntries{
[it.key, it.value.class.name]
}
}
println content
// → [a:java.lang.Integer, b:java.lang.Integer, c:java.lang.Integer, x:java.lang.Boolean, d:java.lang.String]
如果输入 JSON 是对象类型(key-value 对),它会被解析为 Map
,因此您可以使用它的方法来检查它。
import groovy.json.JsonSlurper
String pfile = """
[{"AUTO":"bmw",
"HOME":"vest",
"JOB":1},
{"AUTO":"audi",
"HOME":"dest",
"JOB":2},
{"AUTO":"opel",
"HOME":"lest",
"JOB":3}]
"""
def jsonSlurper = new JsonSlurper()
def list = jsonSlurper.parseText pfile
List names = list.inject( new HashSet() ){ res, map ->
res.addAll map.keySet()
res
}.toList()
// get types based on actual values
def types = list.first().values()*.getClass()*.simpleName
assert '[AUTO, JOB, HOME]' == names.toString()
assert '[String, String, Integer]' == types.toString()
//reader.outputLinesSetHeaders names, types
list.each{ e ->
//reader.outputLines names.collect{ e[ it ] }
println names.collect{ e[ it ] }
}
//reader.outputLinesEnd()
这些行被注释掉以避免编译问题。
请帮我解决问题。我在 groovy 中编程(您可以使用 java 示例,它看起来像那里)。 Json来到input,不知道有多少字段。可以有 5 个字段,可能是 10 个,也可能是 50 个。我的任务是处理此 json 和 return 返回的数据,使用:
// Names of dataset columns
def names = ["a", "b", "c"];
// types of return values in each column (for any field (column) json is always String)
def types = ["String", "String", "String"];
// formation of the dataset header
reader.outputLinesSetHeaders (names, types);
// Passing the data itself from json
reader.outputLines ([it.a, it.b, it.c])
// Close the dataset
reader.outputLinesEnd ();
如果我知道传入的json,那么我会提前设置所需的字段名称,“String”的数量,并return通过引用特定的[=]编辑它们的值25=] 字段。下面的示例显示了 3 json 个字段:auto、home、job。因此,每个字段的 3 倍“String”,并将特定字段引用到 return it.auto、it.home、it.job 的值。但是,如果我不知道传入的 json?
,我怎么能做同样的事情呢?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)
//The names of the columns of the dataset (now set statically to show an example; but in my case I don't know json and can't write it this way in advance)
def names = ["AUTO", "HOME", "JOB"];
//return types in each column (for any json, only "String" types)
def types = ["String", "String", "String"];
//формирование заголовка датасета
reader.outputLinesSetHeaders(names,types);
list.each {
//pass the values as a dataset from json (now set statically to show an example; but in my case I don't know json and can't write it this way in advance)
reader.outputLines([it?.AUTO, it?.HOME, it?.JOB]);
}
//closing dataset
reader.outputLinesEnd();
return null;
}
static void main(String... args) {
String pfile = """
[{"AUTO":"bmw",
"HOME":"vest",
"JOB":"bbds"},
{"AUTO":"audi",
"HOME":"dest",
"JOB":"aads"},
{"AUTO":"opel",
"HOME":"lest",
"JOB":"ffds"}]
"""
def SSC = new XM_PARSE_XLS()
def res = SSC.execute(new ResultSetReader(), pfile)
}
}
也许值得将传入的json的所有字段名称收集到一个列表中并指定一个“String”列表(任何传入的json的所有字段都只有“String”)与字段数相同?但是如何做到这一点以及我应该如何传递字段值(it.***)?
假设您只想知道键和值的类型,您 可以创建所有 keys/types 的联合。这假设值类型 因为一个键在所有键上都是相同的。
import groovy.json.JsonSlurper
def data = new JsonSlurper().parseText("""[{"a": 1, "b": 2, "c": 3, "x": true}, {"a": 4, "b": 5, "c": 6, "d": "Hello"}]""")
def content = data.collectEntries{
it.collectEntries{
[it.key, it.value.class.name]
}
}
println content
// → [a:java.lang.Integer, b:java.lang.Integer, c:java.lang.Integer, x:java.lang.Boolean, d:java.lang.String]
如果输入 JSON 是对象类型(key-value 对),它会被解析为 Map
,因此您可以使用它的方法来检查它。
import groovy.json.JsonSlurper
String pfile = """
[{"AUTO":"bmw",
"HOME":"vest",
"JOB":1},
{"AUTO":"audi",
"HOME":"dest",
"JOB":2},
{"AUTO":"opel",
"HOME":"lest",
"JOB":3}]
"""
def jsonSlurper = new JsonSlurper()
def list = jsonSlurper.parseText pfile
List names = list.inject( new HashSet() ){ res, map ->
res.addAll map.keySet()
res
}.toList()
// get types based on actual values
def types = list.first().values()*.getClass()*.simpleName
assert '[AUTO, JOB, HOME]' == names.toString()
assert '[String, String, Integer]' == types.toString()
//reader.outputLinesSetHeaders names, types
list.each{ e ->
//reader.outputLines names.collect{ e[ it ] }
println names.collect{ e[ it ] }
}
//reader.outputLinesEnd()
这些行被注释掉以避免编译问题。