【问题标题】:Converting JSON Object to Java Object in Jersey JAX-RS在 Jersey JAX-RS 中将 JSON 对象转换为 Java 对象
【发布时间】:2019-04-30 02:57:15
【问题描述】:

我正在尝试将 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
      }
    ]
  }
}

【问题讨论】:

  • 你知道它不是一个有效的 JSON,对吧?
  • 是的,但整体结构应该是正确的还是我错了?它应该只是用于可视化。
  • 你能发布你的 JSON

标签: java json jersey jax-rs javabeans


【解决方案1】:

这是使用 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 和另一个名为 websiteVisited 的对象列表。 websiteVisited 还有一些属性,例如 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 + "]";
  }



}

现在让我们定义主要对象学生

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



}

如果您注意到 students 对象具有名为 List websiteVisited 的属性。

现在写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;

}

希望对你有帮助。

【讨论】:

  • 是的,这对我有很大帮助!有一件事还不清楚。如果将“websitesVisited”json 数组更改为这样的 json 对象:“websitesVisited”:{...},您还会在主对象学生中使用 ListwebsitesVisited 还是必须更改数据类型?
  • 如果您只有一个对象,那么您不需要列表,但如果您有多个对象,那么您当然必须使用列表。如果它解决了您的问题,请接受答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-11
  • 2020-12-07
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多