从Jersey客户端响应中读取单个值

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

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:

  1. import org.json.JSONArray;
  2. import org.json.JSONObject;
  3. // Assuming "response" contains the JSON string you provided
  4. JSONObject jsonResponse = new JSONObject(response);
  5. JSONArray itemsArray = jsonResponse.getJSONArray("items");
  6. if (itemsArray.length() > 0) {
  7. String idValue = itemsArray.getJSONObject(0).getString("id");
  8. // Now, "idValue" contains the value of "id"
  9. }

This code extracts the id value from the JSON response under the items array.

英文:

Hi I am calling an API using bellow code

  1. Client client=ClientBuilder.newClient();
  2. WebTarget target = client.target(dynamicEntityURL);
  3. String response = target.request().header("Authorization", "Bearer "+readToken.tokenMethod()
  4. .getAccess_token())
  5. .get(String.class);
  6. System.out.println(response);

Output is :-

  1. {"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

你可以这样做。

  1. JsonObject response = target.request().header("Authorization", "Bearer " + readToken.tokenMethod()
  2. .getAccess_token())
  3. .get(JsonObject.class);
  4. JsonArray jsonArray = response.getJsonArray("items");
  5. System.out.println(jsonArray.getJsonObject(0).getString("id"));
英文:

You can do like this.

  1. JsonObject response = target.request().header("Authorization", "Bearer "+readToken.tokenMethod()
  2. .getAccess_token())
  3. .get(JsonObject.class);
  4. JsonArray jsonArray = response.getJsonArray("items");
  5. System.out.println(jsonArray.getJsonObject(0).getString("id"));

huangapple
  • 本文由 发表于 2020年8月1日 04:52:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63198953.html
匿名

发表评论

匿名网友

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

确定