gpt4 book ai didi

java - 更改 JSON 格式

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

用 spring 我创建了一个这个网络服务

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

这会返回一个 GetUserInfoResponse 列表,如下所示

enter image description here

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

enter image description here

类: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
}

类:获取用户信息

   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
}

最佳答案

这是创建 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":["德里","孟买","类加罗尔"],"key":"城市"}

关于java - 更改 JSON 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45977955/

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