简体   繁体   English

Java Spring Boot-API对象创建

[英]Java Spring Boot - api object creation

I am working on a Java Spring boot api. 我正在研究Java Spring启动api。

when the the call is made to get /api/home 进行呼叫以获取/ api / home时

I want to return this json sample structure. 我想返回这个json示例结构。

      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 因此在MyService中,其中/ api / home得到了RequestMapped,我开始迷上我的类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. 在MyApiHome中-我想使getHomeData中的“ homeObj”成为一个数组,而不是JSONOBject-但随后我开始陷入类型转换等麻烦。我想以这种方式构建它-如果getProfileBreakDown为null或解耦后,它不会附加到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;
    }

}

要创建JSON数组,我们需要使用JSONArray对象,该对象具有JSONObjects列表。

So using JSONArray. 因此,使用JSONArray。

I add to a json blob like a data stack. 我添加到像数据堆栈的json blob。

JSONArray homeObj = new JSONArray();
    if(this.getProfileBreakDown() != null){
        homeObj.add(this.getProfileBreakDown());
    }
    if(this.getCurrentStanding() != null){
        homeObj.add(this.getCurrentStanding());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM