代码之家  ›  专栏  ›  技术社区  ›  The Old County

Java Spring Boot-api对象创建

  •  0
  • The Old County  · 技术社区  · 7 年前

    我正在开发一个Java Spring引导api。

    当调用get/api/home时

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

    -- 我一直在构建各种函数来获得“配置文件分解”和“当前状态”——我想将响应附加到这些函数中,以模拟上述结构。

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

    在MyApiHome中——我想让getHomeData中的“homebj”成为一个数组,而不是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;
        }
    
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   CS_noob    7 年前

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

        2
  •  0
  •   The Old County    7 年前

    所以使用JSONArray。

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