gpt4 book ai didi

java - 在 Jersey JAX-RS 中将 JSON 对象转换为 Java 对象

转载 作者:行者123 更新时间:2023-11-30 05:53:38 25 4
gpt4 key购买 nike

我正在尝试将 json 对象转换为 Rest api 中的 java 对象,但我不知道如何创建我的 java bean。

json对象本身包含多个对象和数组。我必须在 java bean 中放入什么实例变量才能匹配这些变量?我的第一个猜测是另一个 bean ,但这听起来有点困惑,嵌套程度很高。

这是一个用于可视化的示例 json:

{
key1: value,
key2: value,
anotherJsonObject: {
key3: value,
key4: value,
anotherJsonObject: {
key5: value
...
},
anotherJsonArray: [
{
key6: value,
key7: value
},
{
key6: value,
key7: value
}
]
}
}

最佳答案

这是使用 JAX-RS 的完整示例

首先让我们定义示例 JSON。

[{
"id": 1,
"firstName": "Jeanette",
"lastNname": "Penddreth",
"email": "jpenddreth0@census.gov",
"gender": "Female",
"ipAddress": "26.58.193.2",
"websitesVisited": [{
"websiteName": "www.youtube.com",
"IpAddress": "26.58.193.6",
"timeSpent": "1 Hr",
"NoOfTimeVisitedInDay": "10"
},
{
"websiteName": "www.facebook.com",
"IpAddress": "26.58.193.10",
"timeSpent": "2 Hr",
"NoOfTimeVisitedInDay": "20"
}
]

}

, {
"id": 2,
"firstName": "Giavani",
"lastName": "Frediani",
"email": "gfrediani1@senate.gov",
"gender": "Male",
"ipAddress": "229.179.4.212",
"websitesVisited": [{
"websiteName": "www.youtube.com",
"IpAddress": "26.58.193.6",
"timeSpent": "1 Hr",
"NoOfTimeVisitedInDay": "10"
},
{

"websiteName": "www.facebook.com",
"IpAddress": "26.58.193.10",
"timeSpent": "2 Hr",
"NoOfTimeVisitedInDay": "20"
}

]
}, {
"id": 3,
"firstName": "Noell",
"lastName": "Bea",
"email": "nbea2@imageshack.us",
"gender": "Female",
"ipAddress": "180.66.162.255",
"websitesVisited": [{
"websiteName": "www.youtube.com",
"IpAddress": "26.58.193.6",
"timeSpent": "1 Hr",
"NoOfTimeVisitedInDay": "10"
},
{
"websiteName": "www.facebook.com",
"IpAddress": "26.58.193.10",
"timeSpent": "2 Hr",
"NoOfTimeVisitedInDay": "20"
}

]
}, {
"id": 4,
"firstName": "Willard",
"lastName": "Valek",
"email": "wvalek3@vk.com",
"gender": "Male",
"ipAddress": "67.76.188.26",
"websitesVisited": [{
"websiteName": "www.youtube.com",
"IpAddress": "26.58.193.6",
"timeSpent": "1 Hr",
"NoOfTimeVisitedInDay": "10"
},
{
"websiteName": "www.facebook.com",
"IpAddress": "26.58.193.10",
"timeSpent": "2 Hr",
"NoOfTimeVisitedInDay": "20"
}

]
}
]

现在让我们定义 POJO(Plain OLD JAVA OBJECT)

正如我们所看到的,我们的示例 JSON 具有 Students 对象数组,而 Student 对象具有一些属性,例如 id、firstName、lastName、email、gender、ipAddress 和另一个名为 WebsitesVisited 的对象列表。WebsitesVisited 还有一些属性,例如 websiteName、IpAddress、timeSpent、NoOfTimeVisitedInDay

现在让我们定义 POJO

首先定义名为 Website 的内部对象。

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Websites {

String websiteName;
String ipAddress;
String timeSpent;
String NoOfTimeVisitedInDay;

public Websites() {

}

public String getWebsiteName() {
return websiteName;
}
public void setWebsiteName(String websiteName) {
this.websiteName = websiteName;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getTimeSpent() {
return timeSpent;
}
public void setTimeSpent(String timeSpent) {
this.timeSpent = timeSpent;
}
public String getNoOfTimeVisitedInDay() {
return NoOfTimeVisitedInDay;
}
public void setNoOfTimeVisitedInDay(String noOfTimeVisitedInDay) {
NoOfTimeVisitedInDay = noOfTimeVisitedInDay;
}

@Override
public String toString() {
return "Websites [websiteName=" + websiteName + ", ipAddress=" + ipAddress + ", timeSpent=" + timeSpent +
", NoOfTimeVisitedInDay=" + NoOfTimeVisitedInDay + "]";
}



}

现在让我们定义主要对象 Students

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Students {

String id;
String firstName;
String lastName;
String email;
String gender;
String ipAddress;
List < Websites > websitesVisited;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public List < Websites > getWebsitesVisited() {
return websitesVisited;
}
public void setWebsitesVisited(List < Websites > websitesVisited) {
this.websitesVisited = websitesVisited;
}
@Override
public String toString() {
return "Students [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email +
", gender=" + gender + ", ipAddress=" + ipAddress + ", websitesVisited=" + websitesVisited + "]";
}



}

如果您注意到学生对象具有名为“列出已访问网站”的属性。

现在写post方法

@POST
@Consumes({
MediaType.APPLICATION_JSON
})
@Produces({
MediaType.APPLICATION_JSON
})
@Path("JsonPostExample")
public String JsonPostExample(@PathParam("studentId") String studentId, List < Students > studentS) {
System.out.println(studentS.toString());
// Do whatever you want to do with the object
return studentId;

}

希望对您有所帮助。

关于java - 在 Jersey JAX-RS 中将 JSON 对象转换为 Java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53515417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com