Reputation: 89
I am working on a Java Spring boot api.
when the the call is made to get /api/home
I want to return this json sample structure.
var response = [
{
"type": "profile-breakdown",
"order": 0,
"grid-width": 6,
"grid-background": "",
"grid-background-url": "",
"title": "",
"contents": {
"name": "Name1",
"avatar" : 1,
"nextSDQ": 4,
"SQDCount": 3
}
},
{
"type": "current-standing",
"order": 1,
"grid-width": 6,
"grid-background": "",
"grid-background-url": "",
"title": "Your current standing summary",
"contents": {
"0": ["emotional distress", "behavioural difficulties", "hyperactivity and concentration difficulties", "difficulties in getting along with other young people"],
"4": ["kind and helpful behaviour"]
}
}
]
-- I've been building the various functions to get "profile-breakdown" and "current-standing" -- I want to append the responses to these to mimic the above structure.
so in MyService where /api/home gets RequestMapped to I begin to hook into my class MyApiHome
MyApiHome myApiHome = new MyApiHome();
JSONObject homeObj = myApiHome.getHomeData();
in MyApiHome -- I want to make "homeObj" in getHomeData an array as opposed to an JSONOBject - but then I start to fall into trouble with casts etc.. I want to build this in such a way - that if getProfileBreakDown is null or decoupled it isn't appended to the homeObj.
public class MyApiHome {
@SuppressWarnings("unchecked")
public JSONObject getHomeData(){
//build clean home object
JSONObject homeObj = new JSONObject();
homeObj.put("profile", this.getProfileBreakDown());
homeObj.put("currentstanding", this.getCurrentStanding());
//HashMap<List<String>, Object> hashMap = new HashMap<List<String>, Object>();
//hashMap.put())
return homeObj;
}
@SuppressWarnings("unchecked")
public Object getProfileBreakDown(){
//build clean home object
JSONObject contents = new JSONObject();
contents.put("name", "Name1");
contents.put("avatar", 1);
contents.put("nextSDQ", 4);
contents.put("SQDCount", 3);
//build clean home object
JSONObject json = new JSONObject();
json.put("type", "profile-breakdown");
json.put("order", 0);
json.put("grid-width", 6);
json.put("grid-background", "");
json.put("grid-background-url", "");
json.put("title", "");
json.put("contents", contents);
return json;
}
@SuppressWarnings("unchecked")
public Object getCurrentStanding(){
String[] stressArray1 = {"emotional distress", "behavioural difficulties", "hyperactivity and concentration difficulties", "difficulties in getting along with other young people"};
String[] stressArray2 = {"kind and helpful behaviour"};
//build clean home object
JSONObject contents = new JSONObject();
contents.put("0", stressArray1);
contents.put("4", stressArray2);
//build clean home object
JSONObject json = new JSONObject();
json.put("type", "current-standing");
json.put("order", 1);
json.put("grid-width", 6);
json.put("grid-background", "");
json.put("grid-background-url", "");
json.put("title", "Your current standing summary");
json.put("contents", contents);
return json;
}
}
Upvotes: 0
Views: 85
Reputation: 557
To create an array of JSONs, we need to use JSONArray object which has a list of JSONObjects.
Upvotes: 1
Reputation: 89
So using JSONArray.
I add to a json blob like a data stack.
JSONArray homeObj = new JSONArray();
if(this.getProfileBreakDown() != null){
homeObj.add(this.getProfileBreakDown());
}
if(this.getCurrentStanding() != null){
homeObj.add(this.getCurrentStanding());
}
Upvotes: 0