String转换jsonobject格式错误

String转换jsonobject格式错误String转换jsonobject格式错误开发工具与关键技术:java作者:彭浩达撰写时间:2019年7月24日publicvoidUpdataecdrud(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,I…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

                            String转换jsonobject格式错误
开发工具与关键技术:java
作者:彭浩达
撰写时间:2019年 7月 24日

public void Updataecdrud(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{

HttpSession session = request.getSession(true);
String quantity = request.getParameter(“quantity”);
String retailprice = request.getParameter(“retailprice”);
PurDrugMed drugMed = (PurDrugMed)session.getAttribute(“chudruid”);
String msg = “”;
msg = “数据异常,修改失败”;
if(quantity != “”){

BigDecimal quantitys = new BigDecimal(quantity);
drugMed.setQuantity(quantitys);
msg = “数据异常,修改成功”;
}
if(retailprice != “”){

BigDecimal retailprices = new BigDecimal(retailprice);
drugMed.setRetailprice(retailprices);
msg = “数据异常,修改成功”;

	}
	drugMed.setLingshouz(drugMed.getQuantity().multiply(drugMed.getRetailprice()));
	drugMed.setChengbeiz(drugMed.getCostprice().multiply(drugMed.getQuantity()));
	
	session.removeAttribute("chudruid");
	session.setAttribute("chudruid", drugMed);
	
	//request.getRequestDispatcher("/servlet/CrkServlet?type=selectcund").forward(request, response);
	
	JSONObject jsonObject = JSONObject.fromObject(msg);
	response.setCharacterEncoding("utf-8");
	response.setContentType("text/json");
	response.getWriter().write(jsonObject.toString());
	response.getWriter().flush();
	response.getWriter().close();
}

上面的代码看起来没什么问题,但是在msg转换转换成jsonobject的就报错了
报错的原因是格式不对
在这里插入图片描述

Jsonobject的格式{“msg”:“新增失败”,“a”:“1”,“b”:“2”}
在这里插入图片描述

所以把string类型的msg的数据 改成 “{“msg”:“修改成功!”,“state”:“true”}”;
Jsonobject就可以正常转换了
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/172470.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号