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

Converting JSON Object to Java Object in Jersey JAX-RS

我正在尝试将 json 对象转换为静止的 java 对象 api 但我不知道如何创建我的 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 具有学生对象数组,学生对象具有一些属性,例如 id、名字、姓氏、电子邮件、性别、ipAddress 和另一个名为 websitesVisited 的对象列表。 websitesVisited 有更多属性,如 websiteName、IpAddress、timeSpent、NoOfTimeVisitedInDay

现在让我们定义POJO

首先定义名为 Website 的内部 OBJECT。

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 + "]";
  }



}

如果您注意到学生对象具有名为 List 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;

}

希望对您有所帮助。