从JSONObject中获取“id”的最短方式

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

Shortest possible way to get "id" from JSONObject

问题

import org.json.JSONArray;
import org.json.JSONObject;

// Assuming you have the JSON response stored in the 'response' variable
JSONObject jsonResponse = new JSONObject(response.getBody());

JSONArray entriesArray = jsonResponse.getJSONArray("entries");
String[] ids = new String[entriesArray.length()];

for (int i = 0; i < entriesArray.length(); i++) {
    JSONObject entry = entriesArray.getJSONObject(i).getJSONObject("entry");
    ids[i] = entry.getString("id");
}

// Now the 'ids' array contains all the "id" values from the JSON

Note: As of my last training data in September 2021, there is no direct method to achieve this without iterating through the JSONArray in the given JSON structure using Java's org.json library. If you are open to using libraries other than org.json, you might consider using more modern JSON libraries like Jackson or Gson, which offer more flexible ways to handle JSON data.

英文:

I have a org.json.JSONObject like below:

{&quot;entries&quot;:
   [{&quot;entry&quot;:{&quot;createdAt&quot;:&quot;2020-06-25T18:10:22.571+0000&quot;,&quot;isFolder&quot;:false,&quot;isFile&quot;:true,&quot;createdByUser&quot;:{&quot;displayName&quot;:&quot;Administrator&quot;,&quot;id&quot;:&quot;admin&quot;},&quot;modifiedAt&quot;:&quot;2020-09-28T15:42:50.253+0000&quot;,&quot;modifiedByUser&quot;:{&quot;displayName&quot;:&quot;Administrator&quot;,&quot;id&quot;:&quot;admin&quot;},&quot;name&quot;:&quot;1000024_Resume 1-K_User1 (2020-1601307769393).doc&quot;,&quot;id&quot;:&quot;a9aa23ac-3cca-4fd7-9f82-ec31c2b969f0&quot;,&quot;nodeType&quot;:&quot;hr:HR_Type&quot;,&quot;content&quot;:{&quot;sizeInBytes&quot;:48128,&quot;mimeTypeName&quot;:&quot;Microsoft Word&quot;,&quot;mimeType&quot;:&quot;application/msword&quot;,&quot;encoding&quot;:&quot;UTF-8&quot;},&quot;parentId&quot;:&quot;7db2d13f-db92-4401-aff1-cecddd78db45&quot;}},
   {&quot;entry&quot;:{&quot;createdAt&quot;:&quot;2020-06-25T18:10:23.014+0000&quot;,&quot;isFolder&quot;:false,&quot;isFile&quot;:true,&quot;createdByUser&quot;:{&quot;displayName&quot;:&quot;Administrator&quot;,&quot;id&quot;:&quot;admin&quot;},&quot;modifiedAt&quot;:&quot;2020-07-10T20:40:33.123+0000&quot;,&quot;modifiedByUser&quot;:{&quot;displayName&quot;:&quot;Sarah Campbell&quot;,&quot;id&quot;:&quot;SACAMPBELL&quot;},&quot;name&quot;:&quot;Test.DOC&quot;,&quot;id&quot;:&quot;29cfee8d-5614-4c81-9bfa-581334cc39e9&quot;,&quot;nodeType&quot;:&quot;hr:Test_Type&quot;,&quot;content&quot;:{&quot;sizeInBytes&quot;:35328,&quot;mimeTypeName&quot;:&quot;Microsoft Word&quot;,&quot;mimeType&quot;:&quot;application/msword&quot;,&quot;encoding&quot;:&quot;UTF-8&quot;},&quot;parentId&quot;:&quot;79d3b939-b7e9-4bed-be67-428eb5da0f16&quot;}},
   {&quot;entry&quot;:{&quot;createdAt&quot;:&quot;2020-07-10T15:06:06.252+0000&quot;,&quot;isFolder&quot;:false,&quot;isFile&quot;:true,&quot;createdByUser&quot;:{&quot;displayName&quot;:&quot;Test Display&quot;,&quot;id&quot;:&quot;CN158931&quot;},&quot;modifiedAt&quot;:&quot;2020-09-28T15:39:40.349+0000&quot;,&quot;modifiedByUser&quot;:{&quot;displayName&quot;:&quot;Administrator&quot;,&quot;id&quot;:&quot;admin&quot;},&quot;name&quot;:&quot;1000536_Test Display September 2014.doc&quot;,&quot;id&quot;:&quot;9eea5068-48dc-4e1f-9a19-e7d9749ba3db&quot;,&quot;nodeType&quot;:&quot;hr:Test_Type&quot;,&quot;content&quot;:{&quot;sizeInBytes&quot;:58243,&quot;mimeTypeName&quot;:&quot;Microsoft Word&quot;,&quot;mimeType&quot;:&quot;application/msword&quot;,&quot;encoding&quot;:&quot;UTF-8&quot;},&quot;parentId&quot;:&quot;79d3b939-b7e9-4bed-be67-428eb5da0f16&quot;}},
   {&quot;entry&quot;:{&quot;createdAt&quot;:&quot;2020-07-10T21:09:50.889+0000&quot;,&quot;isFolder&quot;:false,&quot;isFile&quot;:true,&quot;createdByUser&quot;:{&quot;displayName&quot;:&quot;Test Display-Name&quot;,&quot;id&quot;:&quot;CN103107&quot;},&quot;modifiedAt&quot;:&quot;2020-07-10T21:11:26.528+0000&quot;,&quot;modifiedByUser&quot;:{&quot;displayName&quot;:&quot;Some-CGT12&quot;,&quot;id&quot;:&quot;CN103107&quot;},&quot;name&quot;:&quot;Test Display123.jpg&quot;,&quot;id&quot;:&quot;df7c76a1-67b9-4673-8fb7-1a2470d42c1d&quot;,&quot;nodeType&quot;:&quot;hr:Test_Type&quot;,&quot;content&quot;:{&quot;sizeInBytes&quot;:237560,&quot;mimeTypeName&quot;:&quot;JPEG Image&quot;,&quot;mimeType&quot;:&quot;image/jpeg&quot;,&quot;encoding&quot;:&quot;UTF-8&quot;},&quot;parentId&quot;:&quot;7db2d13f-db92-4401-aff1-cecddd78db45&quot;}},
   {&quot;entry&quot;:{&quot;createdAt&quot;:&quot;2020-07-10T21:09:51.706+0000&quot;,&quot;isFolder&quot;:false,&quot;isFile&quot;:true,&quot;createdByUser&quot;:{&quot;displayName&quot;:&quot;Test Display-Name&quot;,&quot;id&quot;:&quot;CN103107&quot;},&quot;modifiedAt&quot;:&quot;2020-07-10T21:09:51.706+0000&quot;,&quot;modifiedByUser&quot;:{&quot;displayName&quot;:&quot;Some-TEst2&quot;,&quot;id&quot;:&quot;CN103107&quot;},&quot;name&quot;:&quot;batman.jpg&quot;,&quot;id&quot;:&quot;88ac8b96-5965-4668-9e94-2b2e3509e0f8&quot;,&quot;nodeType&quot;:&quot;hr:HR_Type&quot;,&quot;content&quot;:{&quot;sizeInBytes&quot;:5588,&quot;mimeTypeName&quot;:&quot;JPEG Image&quot;,&quot;mimeType&quot;:&quot;image/jpeg&quot;,&quot;encoding&quot;:&quot;UTF-8&quot;},&quot;parentId&quot;:&quot;79d3b939-b7e9-4bed-be67-428eb5da0f16&quot;}}]
   ,&quot;pagination&quot;:{&quot;maxItems&quot;:100,&quot;hasMoreItems&quot;:false,&quot;totalItems&quot;:5,&quot;count&quot;:5,&quot;skipCount&quot;:0}}

How do I get the value for all "id"s (a9aa23ac-3cca-4fd7-9f82-ec31c2b969f0, 29cfee8d-5614-4c81-9bfa-581334cc39e9, 9eea5068-48dc-4e1f-9a19-e7d9749ba3db, df7c76a1-67b9-4673-8fb7-1a2470d42c1d, 88ac8b96-5965-4668-9e94-2b2e3509e0f8) from above without using for loops. That means I dont want to iterate through all the objects and then get "id" value.

I am trying to do something like:

org.json.JSONObject myJSONObject = new org.json.JSONObject(response.getBody()).getJSONObject(&quot;entries&quot;).getJSONObject(&quot;entry&quot;).getString(&quot;id&quot;);

If possible, I am looking for one-liner using Java 1.6/1.7/1.8 for above.

答案1

得分: 3

我不确定是否有一个单行解决方案,但使用 JSON PATH 库会变得非常简单。
链接:https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path

Configuration cf = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build();
DocumentContext ctx = JsonPath.using(cf).parse(jsonStr);
List<String> ids = ctx.read("$.entries[*].entry.id");
英文:

I am not sure if there is a single liner solution, but it becomes very easy using JSON PATH library
https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path

Configuration cf = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build();
DocumentContext ctx = JsonPath.using(cf).parse(jsonStr);
List&lt;String&gt; ids = ctx.read(&quot;$.entries[*].entry.id&quot;);

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

发表评论

匿名网友

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

确定