gpt4 book ai didi

Java Spring Boot - api对象创建

转载 作者:行者123 更新时间:2023-12-02 12:17:17 25 4
gpt4 key购买 nike

我正在开发 Java Spring boot 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 时,我开始 Hook 到我的类 MyApiHome

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

在 MyApiHome 中 - 我想将 getHomeData 中的“homeObj”设为数组,而不是 JSONOBject - 但随后我开始陷入强制类型转换等麻烦。我想以这样的方式构建它 - 如果 getProfileBreakDown为空或解耦,它不会附加到 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 数组,我们需要使用包含 JSONObject 列表的 JSONArray 对象。

关于Java Spring Boot - api对象创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46080800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com