其他分享
首页 > 其他分享> > jsp传数据到后台处理多余的key value,即JSON类型的String转为JSON,处理好后再转为String

jsp传数据到后台处理多余的key value,即JSON类型的String转为JSON,处理好后再转为String

作者:互联网

	import com.alibaba.fastjson.JSONArray;
	import com.alibaba.fastjson.JSONObject;
	import java.util.List;
	
	@RequestMapping(value = "/{contentId}/newContent", method = RequestMethod.POST)
	public String newContent(@RequestParam(required = true) String dataJson, HttpServletRequest request) throws Exception {
		JSONArray parse = JSONArray.parseArray(dataJson);
		JSONArray json = new JSONArray();
		String jsonStr = "";
		for (int i = 0; i < parse.size(); i++) {
			JSONObject jsonObject = parse.getJSONObject(i);
			jsonObject.remove("parentContentName");//去除多余的key value
			json.add(jsonObject);
			jsonStr = JSONObject.toJSONString(json);
		}
        List<Content> jsonContentList = new ObjectMapper().readValue(jsonStr, new TypeReference<List<Content>>() {//转换为实体类的list  (Content是实体类)
		});
		return "";
	}

标签:jsonStr,String,JSONArray,json,parse,JSON,import,转为
来源: https://blog.csdn.net/qq_41353397/article/details/120475016