英文:
Reading individual value from jersey client response
问题
You can get the value of id
from the JSON output using a JSON library. Here's an example of how you can do it in Java:
import org.json.JSONArray;
import org.json.JSONObject;
// Assuming "response" contains the JSON string you provided
JSONObject jsonResponse = new JSONObject(response);
JSONArray itemsArray = jsonResponse.getJSONArray("items");
if (itemsArray.length() > 0) {
String idValue = itemsArray.getJSONObject(0).getString("id");
// Now, "idValue" contains the value of "id"
}
This code extracts the id
value from the JSON response under the items
array.
英文:
Hi I am calling an API using bellow code
Client client=ClientBuilder.newClient();
WebTarget target = client.target(dynamicEntityURL);
String response = target.request().header("Authorization", "Bearer "+readToken.tokenMethod()
.getAccess_token())
.get(String.class);
System.out.println(response);
Output is :-
{"count":1,"limit":100,"totalResults":1,"hasMore":false,"items":[{"id":"B183C87E-7067-4BC9-BD9D-5AFA145532F3","name":"DynamicEntityTesting","version":"1.0","status":"PUBLISHED"}],"links":[{"rel":"self","href":"https://idcs-oda-9417f93560b94eb8a2e2a4c9aac9a3ff-t0.data.digitalassistant.oci.oc-test.com/api/v1/skills/?name=DynamicEntityTesting"},{"rel":"canonical","href":"https://idcs-oda-9417f93560b94eb8a2e2a4c9aac9a3ff-t0.data.digitalassistant.oci.oc-test.com/api/v1/skills/?name=DynamicEntityTesting"}]}
out of the above output I only need id
which is under items
. I know I can create POJO
and pass it and read the individual value using getter/setter
. But I will have to create POJOs with all parameters of output. I only need id
to pass to next API
. how can I get the value of id
?
答案1
得分: 0
你可以这样做。
JsonObject response = target.request().header("Authorization", "Bearer " + readToken.tokenMethod()
.getAccess_token())
.get(JsonObject.class);
JsonArray jsonArray = response.getJsonArray("items");
System.out.println(jsonArray.getJsonObject(0).getString("id"));
英文:
You can do like this.
JsonObject response = target.request().header("Authorization", "Bearer "+readToken.tokenMethod()
.getAccess_token())
.get(JsonObject.class);
JsonArray jsonArray = response.getJsonArray("items");
System.out.println(jsonArray.getJsonObject(0).getString("id"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论