Json显示列表中对象的名称

时间:2015-10-02 12:34:33

标签: java json spring-mvc

我正在使用spring mvc返回Tagset JSON对象。我有以下两个java对象

public class Tagset {   
    private String tag;
    private String tagDisplayName;
    private List<Case> caseList;
}

public class Case { 
    private String title;
    private String url;
}

我得到的回应是:

{"tag":"Bluetooth",
 "tagDisplayName":"Bluetooth 101",
 "caseList":[
            {"title":"How do I update my Bluetooth?",
             "url":"https://test.test.com"},
            {"title":"How do I delete my Bluetooth?",
             "url":"https://test.test.com"}
            ]
}

我希望为每个案例对象显示案例名称:

{"tag":"Bluetooth",
 "tagDisplayName":"Bluetooth 101",
 "caseList":[
            case:{"title":"How do I update my Bluetooth?",
             "url":"https://test.test.com"},
            case:{"title":"How do I delete my Bluetooth?",
             "url":"https://test.test.com"}
            ]
}

2 个答案:

答案 0 :(得分:0)

你愿意拥有什么是可能的,但json身体将不复存在。您可以为此编写自定义序列化器和反序列化器。

答案 1 :(得分:0)

请尝试为Case类添加以下代码行;

@JsonTypeInfo(include=As.WRAPPER_OBJECT, use=Id.NAME)

这是Jackson API的一部分。通过添加上面的行我可以生成输出为;

{
  "tag" : "Bluetooth",
  "tagDisplayName" : "Bluetooth 101",
  "caseList" : [ {
    "Case" : {
      "title" : "How do I update my Bluetooth?",
      "url" : "https://test.test.com"
    }
  }, {
    "Case" : {
      "title" : "How do I delete my Bluetooth?",
      "url" : "https://test.test.com"
    }
  } ]
}