JSON格式的各种转换
/**
*JSON 格式的解析
*/
// json 去掉转义字符
message = message.replaceAll("\\\\", "");
//转成String类型
String jsonStr = message.substring(message.indexOf("[") + 1,
message.indexOf("]"));
//转成String类型的JSON格式
jsonObject = JSONObject.fromObject(jsonStr);
//通过key进行取值
String responseCode = jsonObject.getString("msg");
mapRes.put("empNameAdd", empNameAdd);
mapRes.put("deptNameAdd", deptNameAdd);
mapRes.put("deptNo", deptNo);
mapRes.put("loginName", loginName);
mapRes.put("empId", empId);
//定义转成json数组,将map集合封装的数据放到json数组里,定义一个jsonObject,
//通过put方法,转给前台
JSONArray ar = new JSONArray();
ar.add(mapRes);
JSONObject oj = new JSONObject();
oj.put("memberInfo", ar);
_result.setData(oj);
/**
**{
"status": 0,
"msg": "ok",
"data": [
"memberInfo":{
{
"deptNo": "103",
"empNameAdd": "李俊",
"empId": "50048",
"loginName": "lj",
"deptNameAdd": "上海健一网大药房连锁经营有限公司小世界药店"
}
}
]
}
*/
@RequestMapping("/insertFromStore")
public void insertFromStore(
HttpServletRequest request,
HttpServletResponse response){
StringBuffer json = new StringBuffer();
String line = null;
String orderMsg = "";
try {
BufferedReader reader = request.getReader();
while((line = reader.readLine()) != null) {
json.append(line);
}
orderMsg = json.toString();
}
catch(Exception e) {
logger.error(e);
}
if (StringUtil.isEmpty(orderMsg)){
this.setResultInfo("-1", "params error!").write(request, response);
return;
}
com.alibaba.fastjson.JSONObject jsonObject = null;
try {
//把String类型的json串转成Json Object对象
jsonObject = com.alibaba.fastjson.JSONObject.parseObject(orderMsg);
} catch (Exception e) {
this.setResultInfo("-1", "params error!").write(request, response);
return;
}
//门店渠道id
int storeMutilId = 46;
//各种取值
String mTel = jsonObject.getString("mTel");
String tranid = jsonObject.getString("tranid");
//取值,拿到json数组
com.alibaba.fastjson.JSONArray artiitem = jsonObject.getJSONArray("artiitem");
String tranDate = jsonObject.getString("tranDate");
String discount = jsonObject.getString("discount");
String deptName = jsonObject.getString("deptName");
String sumSaleAmt = jsonObject.getString("sumSaleAmt");
if(StringUtil.isEmpty(mTel)||StringUtil.isEmpty(tranid)||StringUtil.isEmpty(tranDate)||StringUtil.isEmpty
(discount)||StringUtil.isEmpty(deptName)||StringUtil.isEmpty(sumSaleAmt)){
this.setResultInfo("-1", "params error!").write(request, response);
return;
}
List<OrderItem> itemList = new ArrayList<OrderItem>();
//订单详情信息
//遍历json数组,取值,赋值
for (Object o : artiitem) {
OrderItem item = new OrderItem();
//转成String类型Json
String jsonStr = com.alibaba.fastjson.JSONObject.toJSONString(o);
//转成json格式
com.alibaba.fastjson.JSONObject itemJson = com.alibaba.fastjson.JSONObject.parseObject(jsonStr);
String artiCode = itemJson.getString("artiCode");
String artiQty = itemJson.getString("artiQty");
String saleAmt = itemJson.getString("saleAmt");
String salePrice = itemJson.getString("salePrice");
if(StringUtil.isEmpty(artiCode)||StringUtil.isEmpty(artiQty)||StringUtil.isEmpty(saleAmt)||StringUtil
.isEmpty(salePrice)){
this.setResultInfo("-1", "params error!").write(request, response);
return;
}
if(new BigDecimal(saleAmt).compareTo(new BigDecimal(salePrice).multiply(new BigDecimal(artiQty))) != 0){
this.setResultInfo("3", "单品金额校验失败").write(request, response);
return;
}
item.setGoodsNo(artiCode);
item.setGoodsPrice(new BigDecimal(salePrice));
item.setGoodsAmount(new BigDecimal(artiQty));
item.setGoodsSumFee(new BigDecimal(saleAmt));
itemList.add(item);
}
//订单信息
OrderInfo orderInfo = new OrderInfo();
orderInfo.setMemberMobile(mTel);
orderInfo.setMultiChannelId(storeMutilId);
orderInfo.setMultiChannelOrderId(Long.valueOf(tranid));
orderInfo.setOtherDiscounts(new BigDecimal(discount));
orderInfo.setFinishTime(tranDate);
orderInfo.setOrderFee(new BigDecimal(sumSaleAmt));
ServiceMessage<String> insertResult= null;
String resultMsg = ResultMsg.Common.OK;
try {
insertResult= orderInfoService.insertOrderInfoFromStore(orderInfo,itemList,deptName);
resultMsg = insertResult.getMessage();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
resultMsg = ResultMsg.OrderActionMsg.INSERT_STORE_FAILURE;
} finally {
this.setResult(insertResult,
resultMsg).write(request,
response);
}
}
JSON格式的各种转换的更多相关文章
- C#JSON格式数据的转换
json格式字符串转化为json对象:JObject calculate = (JObject)JsonConvert.DeserializeObject(Rep.Request["data ...
- json中把非json格式的字符串转换成json对象再转换成json字符串
JSON.toJson(str).toString()假如key和value都是整数的时候,先转换成jsonObject对象,再转换成json字符串
- iOS开发之JSON转PLIST(把存储json格式的文件转换成plist文件)
p.p1 { margin: 0.0px 0.0px 0.0px 60.0px; font: 16.0px "PingFang SC"; color: #586e75 } p.p2 ...
- 把json格式的字符串转换成javascript对象或数组
第一种 JSON.parse(jsonString) 第二种 eval("("+jsonString+")") 第三种 var obj=(function ...
- json格式的时间转换
//yyyy-MM-dd HH:mm:SS function JsonDateToDate(jsondate) { var date = new Date(parseInt(jsondate.repl ...
- JSON格式日期的转换
扒来的链接: https://blog.csdn.net/zhang33565417/article/details/99676975 感谢这位哥们儿的分享!
- 将DataSet(DataTable)转换成JSON格式(生成JS文件存储)
public static string CreateJsonParameters(DataTable dt) { /**/ /**/ /**/ /* /*********************** ...
- json格式转换(json,csjon)(天气预报)
json格式数据默认为string,可以使用eval()函数或者json模块将其转换为dict.标准Json字符串必须使用双引号(")而不能使用单引号('),否则从字符串转换成dict类型会 ...
- json和php数组 格式的互相转换
$json_arr = array('WebName'=>'PHP网站开发教程网','WebSite'=>'http://www.jb51.net'); $php_json = json ...
随机推荐
- 【转】C++箴言:理解typename的两个含义
[转载]http://dev.yesky.com/13/2221013.shtml 问题:在下面的 template declarations(模板声明)中 class 和 typename 有什么不 ...
- Android触摸事件的分发机制
---恢复内容开始--- 一.MotionEvent : ACTION_DOWN(下按事件).ACTION_UP(松开事件).ACTION_MOVE(移动事件) 二.三大函数 1.dispatchTo ...
- GitHub——如何更新已经fork的代码
github上有个很方便的功能叫fork,将别人的工程一键复制到自己账号下.这个功能很方便,但有点不足的是,当源项目更新后,你fork的分支并不会一起更新,需要自己手动去更新.下面记录下网上找到的更新 ...
- spring事务分类简述
spring事务的传播行为是面试中经常被问到的问题,要将事务的传播行为与隔离级别熟练的掌握,在实际开发过程中,特别是在并发高.更新数据量大.关系表比较多的情况下,经常会遇到关于事务的问题.首先,要了解 ...
- 手机端禁止iPhone字体放大
/*禁止iphone字体放大 */ html { -webkit-text-size-adjust: none; }
- postgres安装 以及修改postgres 密码
#postgres安装 apt-get install postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3 postgresql-s ...
- linux运维面试题汇总一
1.如何让history历史命令显示命令使用的具体时间? [root@node0 ~]# export HISTTIMEFORMAT='%F %T ' [root@node0 ~]# history ...
- xampp 命令行修改数据库密码
进入xampp 下 ./mysql -u root -p password 进入mysql控制台 UPDATE mysql.user SET Password=PASSWORD('password') ...
- CCAN:C语言的模块仓库
实践中一门编程语言是否有用.好不好,不仅体现在语言本身,更在语言的生态系统:用的人多不多.社区是否活跃互帮互助.语言的相关库和框架质量如何,还有就是已有的模块的质量与数量. CPAN(Comprehe ...
- 三分钟PJ隐藏SSID无线网络
一般来说用户可以通过路由或主机设置来隐藏无线信号的SSID网络信息,在这种情况下我们使用XP系统自带的无线信号扫描工具将看不到该无线网络的踪影,在这种情况下XP系统无线信号管理工具只能够看到将SSID ...