我正在研究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数组,我们需要使用具有JSONObjects列表的JSONArray对象。

10-08 13:04