英文:
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:
{"entries":
[{"entry":{"createdAt":"2020-06-25T18:10:22.571+0000","isFolder":false,"isFile":true,"createdByUser":{"displayName":"Administrator","id":"admin"},"modifiedAt":"2020-09-28T15:42:50.253+0000","modifiedByUser":{"displayName":"Administrator","id":"admin"},"name":"1000024_Resume 1-K_User1 (2020-1601307769393).doc","id":"a9aa23ac-3cca-4fd7-9f82-ec31c2b969f0","nodeType":"hr:HR_Type","content":{"sizeInBytes":48128,"mimeTypeName":"Microsoft Word","mimeType":"application/msword","encoding":"UTF-8"},"parentId":"7db2d13f-db92-4401-aff1-cecddd78db45"}},
{"entry":{"createdAt":"2020-06-25T18:10:23.014+0000","isFolder":false,"isFile":true,"createdByUser":{"displayName":"Administrator","id":"admin"},"modifiedAt":"2020-07-10T20:40:33.123+0000","modifiedByUser":{"displayName":"Sarah Campbell","id":"SACAMPBELL"},"name":"Test.DOC","id":"29cfee8d-5614-4c81-9bfa-581334cc39e9","nodeType":"hr:Test_Type","content":{"sizeInBytes":35328,"mimeTypeName":"Microsoft Word","mimeType":"application/msword","encoding":"UTF-8"},"parentId":"79d3b939-b7e9-4bed-be67-428eb5da0f16"}},
{"entry":{"createdAt":"2020-07-10T15:06:06.252+0000","isFolder":false,"isFile":true,"createdByUser":{"displayName":"Test Display","id":"CN158931"},"modifiedAt":"2020-09-28T15:39:40.349+0000","modifiedByUser":{"displayName":"Administrator","id":"admin"},"name":"1000536_Test Display September 2014.doc","id":"9eea5068-48dc-4e1f-9a19-e7d9749ba3db","nodeType":"hr:Test_Type","content":{"sizeInBytes":58243,"mimeTypeName":"Microsoft Word","mimeType":"application/msword","encoding":"UTF-8"},"parentId":"79d3b939-b7e9-4bed-be67-428eb5da0f16"}},
{"entry":{"createdAt":"2020-07-10T21:09:50.889+0000","isFolder":false,"isFile":true,"createdByUser":{"displayName":"Test Display-Name","id":"CN103107"},"modifiedAt":"2020-07-10T21:11:26.528+0000","modifiedByUser":{"displayName":"Some-CGT12","id":"CN103107"},"name":"Test Display123.jpg","id":"df7c76a1-67b9-4673-8fb7-1a2470d42c1d","nodeType":"hr:Test_Type","content":{"sizeInBytes":237560,"mimeTypeName":"JPEG Image","mimeType":"image/jpeg","encoding":"UTF-8"},"parentId":"7db2d13f-db92-4401-aff1-cecddd78db45"}},
{"entry":{"createdAt":"2020-07-10T21:09:51.706+0000","isFolder":false,"isFile":true,"createdByUser":{"displayName":"Test Display-Name","id":"CN103107"},"modifiedAt":"2020-07-10T21:09:51.706+0000","modifiedByUser":{"displayName":"Some-TEst2","id":"CN103107"},"name":"batman.jpg","id":"88ac8b96-5965-4668-9e94-2b2e3509e0f8","nodeType":"hr:HR_Type","content":{"sizeInBytes":5588,"mimeTypeName":"JPEG Image","mimeType":"image/jpeg","encoding":"UTF-8"},"parentId":"79d3b939-b7e9-4bed-be67-428eb5da0f16"}}]
,"pagination":{"maxItems":100,"hasMoreItems":false,"totalItems":5,"count":5,"skipCount":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("entries").getJSONObject("entry").getString("id");
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<String> ids = ctx.read("$.entries[*].entry.id");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论