使用Jackson将列表转换为json
使用下面的代码,我已将列表转换为json,但格式如下 如下:使用Jackson将列表转换为json,json,spring,rest,Json,Spring,Rest,使用下面的代码,我已将列表转换为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"},{
{"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控制器:
@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;
}
@RequestMapping(“/getgoodowns”)
public@ResponseBody映射
getGodownsBasedOnDistrict(@RequestParam(value=“district\u code”)字符串
地区代码){
列表仓库\列表=空;
Map m=新的HashMap();
字符串异常=null;
尝试
{
//在这里获取名称和代码
仓库列表=供应链服务。GetGodownlistBesEndistCode(地区代码);
}捕获(例外情况除外)
{
例如printStackTrace();
exception=ex.getMessage();
}
如果(货仓清单!=null){
对于(int i=0;i为什么要将列表放入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;
}
@RequestMapping(“/getgoodowns”)
public@ResponseBody List getgoodownsbasedondistrict(@RequestParam(value=“district\u code”)字符串距离码){
列表仓库\列表=空;
字符串异常=null;
试一试{
//在这里获取名称和代码
仓库列表=供应链服务。GetGodownlistBesEndistCode(地区代码);
}捕获(例外情况除外){
例如printStackTrace();
exception=ex.getMessage();
}
返回仓库清单;
}
将返回结果从Map
更改为List
,并放置:retrun goodown\u List
所以,
@RequestMapping(“/getgoodowns”)
public@ResponseBody列表
getGodownsBasedOnDistrict(@RequestParam(value=“district\u code”)字符串
地区代码){
列表仓库列表=新的ArrayList();
字符串异常=null;
尝试
{
//在这里获取名称和代码
仓库列表=供应链服务。GetGodownlistBesEndistCode(地区代码);
}捕获(例外情况除外)
{
例如printStackTrace();
exception=ex.getMessage();
}
返回仓库清单;
}
更新
您可以将结果作为字符串返回,您将得到所需的:
@RequestMapping("/getGodowns")
public @ResponseBody String
getGodownsBasedOnDistrict(@RequestParam(value="district_code") String
dist_code) {
List<CscGodownBean> godown_list = new ArrayList<CscGodownBean>();
String exception = null;
try
{
//getting name and codes here
godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
}catch(Exception ex)
{
ex.printStackTrace();
exception = ex.getMessage();
}
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
String arrayToJson = objectMapper.writeValueAsString(godown_list);
System.out.println("Convert List to JSON :");
System.out.println(arrayToJson);
return arrayToJson ;
}
@RequestMapping(“/getgoodowns”)
public@ResponseBody字符串
getGodownsBasedOnDistrict(@RequestParam(value=“district\u code”)字符串
地区代码){
列表仓库列表=新的ArrayList();
字符串异常=null;
尝试
{
//在这里获取名称和代码
仓库列表=供应链服务。GetGodownlistBesEndistCode(地区代码);
}捕获(例外情况除外)
{
例如printStackTrace();
exception=ex.getMessage();
}
ObjectMapper ObjectMapper=新的ObjectMapper();
enable(SerializationFeature.INDENT_输出);
字符串arrayToJson=objectMapper.writeValueAsString(仓库列表);
System.out.println(“将列表转换为JSON:”);
System.out.println(arrayToJson);
返回arrayToJson;
}
返回的字符串是json格式。您将返回类型添加为Map,但仍然需要与ajaxComplete()put代码相同的返回类型
var response = '{"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"}]}'
JSON.stringify(JSON.parse(response).GodownMaster);
以下是我使用的:
@RequestMapping("/alluserreportJSON")
public @ResponseBody String getusersJSON() {
ObjectMapper objectMapper = new ObjectMapper();
//Set pretty printing of json
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
List<AppUser> userlist = null;
@SuppressWarnings("unused")
String exception = null;
String arrayToJson = null;
try {
userlist = userService.findAllUsers();
arrayToJson = objectMapper.writeValueAsString(userlist);
} catch (Exception ex) {
ex.printStackTrace();
exception = ex.getMessage();
}
return arrayToJson;
}
@RequestMapping(“/alluserreportJSON”)
public@ResponseBody字符串getusersJSON(){
ObjectMapper ObjectMapper=新的ObjectMapper();
//设置json的打印
enable(SerializationFeature.INDENT_输出);
List.我想返回如上所述的json数组格式,而不是将listList转换为json数组。尝试一下@Leffchik提到的,List
是自然映射到json数组的Java结构之一。Map
另一方面,不映射(双关语)当然是JSON数组,因为您必须丢弃所有密钥。@Leffchik,这就是我要问的,我已经将它转换为{“GodownMaster”:[{“pname”:“FCI CHARLAPALLI”,“pcode”:“16042”}}},但我希望它是[{“pname”:“FCI CHARLAPALLI”,“pcode”:“16042”}],{“pname”:“FCI CHARLAPALLI123”,“pcode”:“16042123”}]这意味着我只想删除{“GoDeNeMrd:”:“请在下面的投票中添加解释注释:”不列出,我想把列表转换成JSON数组格式,比如[{pCalNe]:“FCI CabLaLali”,“pCo码”:“pNeX”,“MLS圈1 L.Naar”,“pCad”:“16016”},{“pNeX”:“MLS圈4 AZAMABAD”,“pCe码”:“16003”},{pNeN}:“MLS CIRCLE 6 VIDYANAGAR”,“pcode:“16005”},{“pname:“OTHERS”,“pcode:“1699”}]然后我认为不可能只返回映射返回类型的列表。该列表将自动转换为JSON,并使用Jackson@TouzeneMohamedWassim,我已将它转换为{“GodownMaster”:[{“pname”:“FCI CHARLAPALLI”,“pcode”:“16042”}}}但我希望它是[{“pname”“:”FCI CHARLAPALLI“,”pcode“:”16042“}],“{”pname“:”FCI CHARLAPALLI123“,”pcode“:”16042123“}]这意味着我只想删除“{”GodownMaster“:}我已经更新了我的答案。如果需要,请告诉我。我想在控制器中执行此操作,并返回输出为[{”pname:“FCI CHARLAPALLI123“,”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”}]
@RequestMapping("/alluserreportJSON")
public @ResponseBody String getusersJSON() {
ObjectMapper objectMapper = new ObjectMapper();
//Set pretty printing of json
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
List<AppUser> userlist = null;
@SuppressWarnings("unused")
String exception = null;
String arrayToJson = null;
try {
userlist = userService.findAllUsers();
arrayToJson = objectMapper.writeValueAsString(userlist);
} catch (Exception ex) {
ex.printStackTrace();
exception = ex.getMessage();
}
return arrayToJson;
}