gpt4 book ai didi

json - 使用 Jackson 将列表转换为 json

转载 作者:行者123 更新时间:2023-12-04 11:07:19 25 4
gpt4 key购买 nike

使用以下代码,我已将列表转换为 json,但格式为
如下:

{"GodownMaster":[{"pname":"FCI CHARLAPALLI","pcode":"16042"},
{"pname":"MLS CIRCLE 1 L.B. NAGAR","pcode":"16016"},{"pname":"MLS CIRCLE 4
AZAMABAD","pcode":"16003"},{"pname":"MLS CIRCLE 6
VIDYANAGAR","pcode":"16005"},{"pname":"OTHERS","pcode":"1699"}]}

但我想将其转换为:
[{"pname":"FCI CHARLAPALLI","pcode":"16042"},
{"pname":"MLS CIRCLE 1 L.B. NAGAR","pcode":"16016"},{"pname":"MLS CIRCLE 4
AZAMABAD","pcode":"16003"},{"pname":"MLS CIRCLE 6
VIDYANAGAR","pcode":"16005"},{"pname":"OTHERS","pcode":"1699"}]

下面是我的 Spring Controller :
@RequestMapping("/getGodowns")
public @ResponseBody Map
getGodownsBasedOnDistrict(@RequestParam(value="district_code") String
dist_code) {

List<CscGodownBean> godown_list = null;
Map<String, List<CscGodownBean>> m = new HashMap();
String exception = null;
try
{
//getting name and codes here
godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
}catch(Exception ex)
{
ex.printStackTrace();
exception = ex.getMessage();
}

if(godown_list!=null) {
for(int i=0;i<godown_list.size();i++) {
m.put("GodownMaster",godown_list);
}
}
return m;
}

最佳答案

你为什么要把你的名单放到Map ?代码看起来很奇怪。如果你想返回一个列表,就这样做:

@RequestMapping("/getGodowns")
public @ResponseBody List<CscGodownBean> getGodownsBasedOnDistrict(@RequestParam(value="district_code") String dist_code) {
List<CscGodownBean> godown_list = null;
String exception = null;
try {
//getting name and codes here
godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
} catch (Exception ex) {
ex.printStackTrace();
exception = ex.getMessage();
}
return godown_list;
}

关于json - 使用 Jackson 将列表转换为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44802278/

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