【问题标题】:changing JSON format更改 JSON 格式
【发布时间】:2023-03-18 18:21:02
【问题描述】:

使用 spring 我创建了一个这个 web 服务

@POST
    @Path("/get_user_info")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public List<GetUserInfoResponse> get_User_Info(GetUserInfoRequest request) throws Exception;

这会返回给我一个GetUserInfoResponse 的列表,就像这样

问题:是否有可能得到这样的 JSON ???? :

类:GetUserInfoResponse

package com.audaxis.compiere.ws.bean.response;

//Same imports

@XmlRootElement(name="infos")
@XmlType(propOrder={"key", "values"})
public class GetUserInfoResponse {

    private int key;
    private List<GetUserInfo> values;

   //Same Constructor
   //Same getters && setters
}

类:GetUserInfo

   package com.audaxis.compiere.ws.bean;
    //Same imports

    @XmlRootElement(name="values")
    @XmlType(propOrder={"columnName", "old_value", "new_value", "status", "motif"})
    public class GetUserInfo {

        private String columnName;
        private String old_value;
        private String new_value;
        private String status;
        private String motif;

//Same Constructor
//Same getters && setters

 }

这是我的程序:

methode(){
    List<GetUserInfoResponse> responses = new ArrayList<GetUserInfoResponse>();
    while(rs.next()){
    GetUserInfoResponse response = new GetUserInfoResponse();
      for (X_Z_WS_Column column : columns) {
        GetUserInfo info = new GetUserInfo();
        //setinfo
        infos.add(info);
    }
    response.setValues(infos);
    responses.add(response);
   }
   return responses
 }

【问题讨论】:

    标签: java json spring web-services


    【解决方案1】:

    这是制作 json 对象并填充它的基本 java 代码。

    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    
    public class test {
        public static void main(String[] args) {
    
            JSONObject obj = new JSONObject();
            obj.put("key", "city");
    
            JSONArray list = new JSONArray();
            list.add("Delhi");
            list.add("Mumbai");
            list.add("Bangalore");
            obj.put("value", list);
    
            System.out.print(obj);
    
        }
    
    }
    

    如果您对格式有任何疑问,欢迎提出。 输出是:

    {"value":["Delhi","Mumbai","Bangalore"],"key":"city"}

    【讨论】:

    • 我不会像这样创建 json,我在 lib jackson 之上实例化 bean,我认为将 bean 转换为 json
    • 你能把你的整个程序贴出来吗,我没有得到你?
    • 这一行列的值是多少? “对于(X_Z_WS_Column 列:列)”。你在哪里填充对象?
    • 这个返回包含值的列(每一列都有一个值)
    • 然后,您可以在填充列的任何位置删除“信息列”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多