簡體   English   中英

Java Spring Boot-API對象創建

[英]Java Spring Boot - api object creation

我正在研究Java Spring啟動api。

進行呼叫以獲取/ api / home時

我想返回這個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"]
              }
            }
]

-我一直在構建各種功能來獲取“概要文件分解”和“當前狀態”-我想在這些附加響應以模仿上述結構。

因此在MyService中,其中/ api / home得到了RequestMapped,我開始迷上我的類MyApiHome

    MyApiHome myApiHome = new MyApiHome();
    JSONObject homeObj = myApiHome.getHomeData();

在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列表。

因此,使用JSONArray。

我添加到像數據堆棧的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