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

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

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"));

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:

确定