我在eclipse中创建了一个休息api作为maven项目。 用于rest api的MobileAnalyticsModel类是
package org.subhayya.amazonws.mobileanalytics;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class MobileAnalyticsModel {
private String name;
private Date created;
private String location;
private String prize;
private String requirement;
public MobileAnalyticsModel() {
}
public MobileAnalyticsModel(String name, String location, String prize, String requirement) {
this.name = name;
this.location = location;
this.prize = prize;
this.requirement = requirement;
this.created = new Date();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getPrize() {
return prize;
}
public void setPrize(String prize) {
this.prize = prize;
}
public String getRequirement() {
return requirement;
}
public void setRequirement(String requirement) {
this.requirement = requirement;
}
}
这是创建的api的json响应:
和 这是我调用rest api的示例测试代码:
package org.subhayya.example;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
public class SampleTestREstClient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient( );
String reponse = client.target("http://localhost:8080/AgentBasedCloudServiceCompositionFramework/webapi/mobileanalytics/mobileanalyticsjson")
.request(MediaType.APPLICATION_JSON)
.get(String.class);
System.out.println(reponse);
}}
然后我得到了完整的json响应..作为
[{"created":"2017-03-30T14:36:58.56","location":"http://api.server.com","name":"Mobile Analytics","prize":"$1.00 per 1,000,000 Amazon Mobile Analytics events per month thereafter","requirement":"PutEvents"}]
但我希望将单个参数作为我的输出,例如,名称,位置或要求。我也在同一个maven项目中创建客户端调用代码。所以我编写了如下代码
Client client = ClientBuilder.newClient( );
MobileAnalyticsModel reponse =
client.target("http://localhost:8080/AgentBasedCloudServiceCompositionFramework/webapi/mobileanalytics/mobileanalyticsjson")
.request(MediaType.APPLICATION_JSON)
.get(MobileAnalyticsModel.class);
System.out.println(reponse.getName());
但我得到例外,所以我将其改为System.out.println(reponse);
获得至少JSON响应,然后也得到错误。
如何从JSON响应中获取单个名称参数?我是新来的这个休息api ..请帮助我尽快解决这个问题。谢谢提前
答案 0 :(得分:1)
您的回复是一个字符串。访问JSON响应元素的最简单方法是将共振转换为Json-Object。然后,您可以通过名称轻松访问这些字段。 看一下: How to parse JSON in Java
答案 1 :(得分:0)
您还可以查看以下链接,将json转换为对象。
答案 2 :(得分:0)
此代码适用于我..
String url = "http://localhost:8080/AgentBasedCloudServiceCompositionFramework/webapi/mobileanalytics/";
String city = "mobileanalyticsjson";
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.register(JsonProcessingFeature.class).target(url);
JsonArray jsonArray = webTarget.path(city)
.request(MediaType.APPLICATION_JSON_TYPE).get(JsonArray.class);
for (JsonObject jsonObject : jsonArray.getValuesAs(JsonObject.class)) {
System.out.println(jsonObject.getString("name"));
System.out.println(jsonObject.getString("location")); }