1

I'm using spring mvc to return a Tagset JSON object. I have the two following java objects

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

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

The response I'm getting is:

{"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"}
            ]
}

What I would like is for the case name to show for every case object:

{"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"}
            ]
}
Mircea Badescu
  • 291
  • 3
  • 7
  • 16
  • 1
    You may find this helpful http://stackoverflow.com/questions/2435527/use-class-name-as-root-key-for-json-jackson-serialization – Filip Oct 02 '15 at 12:36
  • case should be in quotation marks right? – hevi Oct 02 '15 at 14:35
  • Even though we can add obj type "case" to "caseList" it is not necessary to iterate the JSON obj. caseList[i].title will give same result as caseList[0].case.title. Also if your object grows into larger data set amount of data transferred from server to UI will increase manifold, hence keep your JSON output as minimum as possible and only to the required data. – NightsWatch Oct 02 '15 at 14:37

2 Answers2

0

What you are willing to have is possible but it will be no more is json body. You can write your custom serializer and de-serializer for this purpose.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
0

Please try adding following line of code for Case class;

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

This is part of Jackson API. By adding above line I am able to generate output as;

{
  "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"
    }
  } ]
}
NightsWatch
  • 467
  • 1
  • 6
  • 16