从 json 对象中提取特定数据并将它们存储在列表中
Extract specific data from json object and store them in a list
我是 json 格式世界的新手。我将 Json 信息存储在 json 对象中,我只想提取列表中的名称键值。至少我有一个用户,有时不止一个用户。使用 Java 或 Groovy.
提取
{
"reviewers": [
{
"user": {
"name": "name1.n1",
"emailAddress": "example@example.com"
},
"role": "REVIEWER"
},
{
"user": {
"name": "name2.n2",
"emailAddress": "example2@example.com"
},
"role": "REVIEWER"
}
]
}
将 Java 与库 org.json.JSONObject 一起使用;
JSONObject json =new JSONObject(YOUR_JSON_HERE );
JSONArray array = json.getJSONArray("reviewers" );
for(int i=0;i<array.length();i++){
JSONObject user =array.getJSONObject(i);
System.out.println(user.getJSONObject("user").get("name"));
}
}
您只需使用 Groovy:
即可获得这样的姓名列表
jason = '''{
"reviewers": [
{
"user": {
"name": "name1.n1",
"emailAddress": "example@example.com"
},
"role": "REVIEWER"
},
{
"user": {
"name": "name2.n2",
"emailAddress": "example2@example.com"
},
"role": "REVIEWER"
}
]
}
'''
import groovy.json.JsonSlurper
def jsonslurper = new JsonSlurper()
def object = jsonslurper.parseText(jason)
List names = object.findAll { it.value instanceof List }
.values()
.flatten()
.collect { it.user.name }
println names
基本 groovy+json 文档在这里:https://groovy-lang.org/json.html
import groovy.json.JsonSlurper
def json = '''{
"reviewers": [
{
"user": {
"name": "name1.n1",
"emailAddress": "example@example.com"
},
"role": "REVIEWER"
},
{
"user": {
"name": "name2.n2",
"emailAddress": "example2@example.com"
},
"role": "REVIEWER"
}
]
}
'''
def obj = new JsonSlurper().parseText(json)
println obj.reviewers.collect{ it.user.name } // v1
println obj.reviewers*.user.name // the same as above but shorter
我是 json 格式世界的新手。我将 Json 信息存储在 json 对象中,我只想提取列表中的名称键值。至少我有一个用户,有时不止一个用户。使用 Java 或 Groovy.
提取{
"reviewers": [
{
"user": {
"name": "name1.n1",
"emailAddress": "example@example.com"
},
"role": "REVIEWER"
},
{
"user": {
"name": "name2.n2",
"emailAddress": "example2@example.com"
},
"role": "REVIEWER"
}
]
}
将 Java 与库 org.json.JSONObject 一起使用;
JSONObject json =new JSONObject(YOUR_JSON_HERE );
JSONArray array = json.getJSONArray("reviewers" );
for(int i=0;i<array.length();i++){
JSONObject user =array.getJSONObject(i);
System.out.println(user.getJSONObject("user").get("name"));
}
}
您只需使用 Groovy:
即可获得这样的姓名列表jason = '''{
"reviewers": [
{
"user": {
"name": "name1.n1",
"emailAddress": "example@example.com"
},
"role": "REVIEWER"
},
{
"user": {
"name": "name2.n2",
"emailAddress": "example2@example.com"
},
"role": "REVIEWER"
}
]
}
'''
import groovy.json.JsonSlurper
def jsonslurper = new JsonSlurper()
def object = jsonslurper.parseText(jason)
List names = object.findAll { it.value instanceof List }
.values()
.flatten()
.collect { it.user.name }
println names
基本 groovy+json 文档在这里:https://groovy-lang.org/json.html
import groovy.json.JsonSlurper
def json = '''{
"reviewers": [
{
"user": {
"name": "name1.n1",
"emailAddress": "example@example.com"
},
"role": "REVIEWER"
},
{
"user": {
"name": "name2.n2",
"emailAddress": "example2@example.com"
},
"role": "REVIEWER"
}
]
}
'''
def obj = new JsonSlurper().parseText(json)
println obj.reviewers.collect{ it.user.name } // v1
println obj.reviewers*.user.name // the same as above but shorter