Java 如何使用resful服务返回json数组
这里代码返回JSONJava 如何使用resful服务返回json数组,java,arrays,json,spring-mvc,Java,Arrays,Json,Spring Mvc,这里代码返回JSON @RequestMapping(method = RequestMethod.GET, value="/json") public @ResponseBody employee json(HttpServletRequest request) { String name = request.getParameter("name"); employee ep = new employee(); ep.setResult(name); retur
@RequestMapping(method = RequestMethod.GET, value="/json")
public @ResponseBody employee json(HttpServletRequest request) {
String name = request.getParameter("name");
employee ep = new employee();
ep.setResult(name);
return ep;
}
班级员工:
public class employee {
String result;
public employee(){}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public employee(String result) {
this.result = result;
}
}
当我调用url时:
我的结果是{“result”:“abc”}
但我的期望是{“employee”:[{“result”:“abc”}}
那么我该怎么做呢?您可以得到以下输出:
{
"employee": {
"result": "abc"
}
}
通过注释您的员工
类别:
@JsonTypeInfo(include=JsonTypeInfo.As.WRAPPER_OBJECT, use=Id.NAME)
public class employee {
// same class body
}
您可以获得以下输出:
{
"employee": {
"result": "abc"
}
}
通过注释您的员工
类别:
@JsonTypeInfo(include=JsonTypeInfo.As.WRAPPER_OBJECT, use=Id.NAME)
public class employee {
// same class body
}
首先,为了使类属性能够映射到JSON数组,该属性应该是集合类型,如List、Set、Map等
public class employee {
List<String> result;
public employee(){}
public List<String> getResult() {
return result;
}
public void setResult(List<String> result) {
this.result = result;
}
public employee(String result) {
this.result = result;
}
}
公共类员工{
列出结果;
公共雇员(){}
公共列表getResult(){
返回结果;
}
公共void setResult(列表结果){
this.result=结果;
}
公共雇员(字符串结果){
this.result=结果;
}
}
然后Spring将结果属性映射到数组。在您的情况下,由于结果属性不是集合,因此不需要将其映射到JSON数组
即使在类中使用集合,也会得到类似的结果
{“结果”:[“abc”]}
但不是{employee:{result:[“abc”]}
。要获得所需的结果,您需要一个包装器对象。您可以参考Gary对此部分的回答。首先,要使类属性能够映射到JSON数组,该属性应该是集合类型,如List、Set、Map等。因此,如果您将employee类定义为
public class employee {
List<String> result;
public employee(){}
public List<String> getResult() {
return result;
}
public void setResult(List<String> result) {
this.result = result;
}
public employee(String result) {
this.result = result;
}
}
公共类员工{
列出结果;
公共雇员(){}
公共列表getResult(){
返回结果;
}
公共void setResult(列表结果){
this.result=结果;
}
公共雇员(字符串结果){
this.result=结果;
}
}
然后Spring将结果属性映射到一个数组。在您的情况下,由于结果属性不是集合,因此无需将其映射到JSON数组
即使在类中使用集合,也会得到类似的结果
{“result”:[“abc”]}
但不是{employee:{result:[“abc”]}
。要获得所需的结果,您需要一个包装器对象。您可以引用Gary对此部分的答案。您需要具有以下类型的类才能获得预期的json结果:
具有结果属性的类:
class Second{
String result;
public Second(String r){
this.result = r;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
类,该类包含具有属性结果的类的列表:
class Employee{
List<Second> employee = new ArrayList<Second>();
public List<Second> getEmployee() {
return employee;
}
public void setEmployee(List<Second> s) {
this.employee = s;
}
}
为了获得预期的json结果,您需要具有以下类型的类: 具有结果属性的类:
class Second{
String result;
public Second(String r){
this.result = r;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
类,该类包含具有属性结果的类的列表:
class Employee{
List<Second> employee = new ArrayList<Second>();
public List<Second> getEmployee() {
return employee;
}
public void setEmployee(List<Second> s) {
this.employee = s;
}
}
尝试
List
作为返回类型并返回ArrayList
。另外,我认为预期的JSON不是一个有效的JSON。我修复了。尝试List
作为返回类型并返回ArrayList
。另外,我认为预期的JSON不是一个有效的JSON。我修复了。什么“as”代表?@Scribbler..更新了答案。“As”代表什么?@Scribbler..更新了答案。