从JSON对象中提取特定数据并将其存储在列表中。

huangapple go评论89阅读模式
英文:

Extract specific data from json object and store them in a list

问题

[
  {
    "user": {
      "name": "name1.n1",
      "emailAddress": "example@example.com"
    },
    "role": "REVIEWER"
  },
  {
    "user": {
      "name": "name2.n2",
      "emailAddress": "example2@example.com"
    },
    "role": "REVIEWER"
  }
]

使用Java或Groovy提取json对象中的姓名(name)键的值,将其存储在列表中。可能有一个或多个用户。

英文:

I´m new to the world of json format. I have Json info stored in a json object and I only want to extract name key values in a list. At least I have one user and sometimes more than one user. Extraction using Java or Groovy.

{
  "reviewers": [
    {
      "user": {
        "name": "name1.n1",
        "emailAddress": "example@example.com"
      },
      "role": "REVIEWER"
    },
    {
      "user": {
        "name": "name2.n2",
        "emailAddress": "example2@example.com"
      },
      "role": "REVIEWER"
    }
  ]
}

答案1

得分: 2

使用带有库org.json.JSONObject的Java;

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"));
}
英文:

Using Java with library org.json.JSONObject;

   JSONObject json =new JSONObject(YOUR_JSON_HERE );        
            JSONArray array = json.getJSONArray(&quot;reviewers&quot; );
            for(int i=0;i&lt;array.length();i++){
                JSONObject user =array.getJSONObject(i);   
                System.out.println(user.getJSONObject(&quot;user&quot;).get(&quot;name&quot;));
            }
            
    
        }

答案2

得分: 2

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

<details>
<summary>英文:</summary>

basic groovy+json doc here: 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


</details>



# 答案3
**得分**: 0

```python
您可以像这样使用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
英文:

You can get a list of names like this, using just Groovy:

jason = &#39;&#39;&#39;{
  &quot;reviewers&quot;: [
    {
      &quot;user&quot;: {
        &quot;name&quot;: &quot;name1.n1&quot;,
        &quot;emailAddress&quot;: &quot;example@example.com&quot;
      },
      &quot;role&quot;: &quot;REVIEWER&quot;
    },
    {
      &quot;user&quot;: {
        &quot;name&quot;: &quot;name2.n2&quot;,
        &quot;emailAddress&quot;: &quot;example2@example.com&quot;
      },
      &quot;role&quot;: &quot;REVIEWER&quot;
    }
  ]
}
&#39;&#39;&#39;


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

huangapple
  • 本文由 发表于 2020年9月16日 18:52:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63918467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定