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

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

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

问题

  1. [
  2. {
  3. "user": {
  4. "name": "name1.n1",
  5. "emailAddress": "example@example.com"
  6. },
  7. "role": "REVIEWER"
  8. },
  9. {
  10. "user": {
  11. "name": "name2.n2",
  12. "emailAddress": "example2@example.com"
  13. },
  14. "role": "REVIEWER"
  15. }
  16. ]

使用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.

  1. {
  2. "reviewers": [
  3. {
  4. "user": {
  5. "name": "name1.n1",
  6. "emailAddress": "example@example.com"
  7. },
  8. "role": "REVIEWER"
  9. },
  10. {
  11. "user": {
  12. "name": "name2.n2",
  13. "emailAddress": "example2@example.com"
  14. },
  15. "role": "REVIEWER"
  16. }
  17. ]
  18. }

答案1

得分: 2

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

  1. JSONObject json = new JSONObject(YOUR_JSON_HERE);
  2. JSONArray array = json.getJSONArray("reviewers");
  3. for (int i = 0; i < array.length(); i++) {
  4. JSONObject user = array.getJSONObject(i);
  5. System.out.println(user.getJSONObject("user").get("name"));
  6. }
英文:

Using Java with library org.json.JSONObject;

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

答案2

得分: 2

  1. import groovy.json.JsonSlurper
  2. def json = '''{
  3. "reviewers": [
  4. {
  5. "user": {
  6. "name": "name1.n1",
  7. "emailAddress": "example@example.com"
  8. },
  9. "role": "REVIEWER"
  10. },
  11. {
  12. "user": {
  13. "name": "name2.n2",
  14. "emailAddress": "example2@example.com"
  15. },
  16. "role": "REVIEWER"
  17. }
  18. ]
  19. }
  20. '''
  21. def obj = new JsonSlurper().parseText(json)
  22. println obj.reviewers.collect{ it.user.name } // v1
  23. println obj.reviewers*.user.name // the same as above but shorter
  1. <details>
  2. <summary>英文:</summary>
  3. 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

  1. </details>
  2. # 答案3
  3. **得分**: 0
  4. ```python
  5. 您可以像这样使用Groovy获取名称列表:
  6. jason = '''{
  7. "reviewers": [
  8. {
  9. "user": {
  10. "name": "name1.n1",
  11. "emailAddress": "example@example.com"
  12. },
  13. "role": "REVIEWER"
  14. },
  15. {
  16. "user": {
  17. "name": "name2.n2",
  18. "emailAddress": "example2@example.com"
  19. },
  20. "role": "REVIEWER"
  21. }
  22. ]
  23. }'''
  24. import groovy.json.JsonSlurper
  25. def jsonslurper = new JsonSlurper()
  26. def object = jsonslurper.parseText(jason)
  27. List names = object.findAll { it.value instanceof List }
  28. .values()
  29. .flatten()
  30. .collect { it.user.name }
  31. println names
英文:

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

  1. jason = &#39;&#39;&#39;{
  2. &quot;reviewers&quot;: [
  3. {
  4. &quot;user&quot;: {
  5. &quot;name&quot;: &quot;name1.n1&quot;,
  6. &quot;emailAddress&quot;: &quot;example@example.com&quot;
  7. },
  8. &quot;role&quot;: &quot;REVIEWER&quot;
  9. },
  10. {
  11. &quot;user&quot;: {
  12. &quot;name&quot;: &quot;name2.n2&quot;,
  13. &quot;emailAddress&quot;: &quot;example2@example.com&quot;
  14. },
  15. &quot;role&quot;: &quot;REVIEWER&quot;
  16. }
  17. ]
  18. }
  19. &#39;&#39;&#39;
  20. import groovy.json.JsonSlurper
  21. def jsonslurper = new JsonSlurper()
  22. def object = jsonslurper.parseText(jason)
  23. List names = object.findAll { it.value instanceof List }
  24. .values()
  25. .flatten()
  26. .collect { it.user.name }
  27. 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:

确定