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 ...
随机推荐
- XFire构建服务端Service的两种方式
1.原声构建: 2.集成spring构建 http://blog.csdn.net/carefree31441/article/details/4000436XFire构建服务端Service的两种方 ...
- phpMyAdmim无法打开或空白页面问题解决
环境:windows环境 安装方式:appserv 安装完appserv之后,发现phpMyAdmin无法打开,具体表现为输入root用户名和密码之后长时间无法进入管理页面或进入之后一片空白.这种情况 ...
- mahout学习-1
一. 安装软件 需要安装如下文件: java, Eclipse, Maven,Hadoop,mahout 二. 推荐系统简介 每天,我们都会对一些事物表达自己的看法,喜欢,或不喜欢,或不在乎.这些都在 ...
- jquery中的replaceWith()和html()有什么区别?
区别在于,html()会替换指定元素内部的HTML,而replaceWith()会替换元素本身及其内部的HTML. 例子: 1 <div id="myid" /> 1 ...
- 史上最全的css hack
<!DOCTYPE html> <html> <head> <title>Css Hack</title> <style> #t ...
- string string.h=cstring=str
<string.h> <string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <cstring> 在C++标准化(1998年 ...
- salt-grains
自定义grains 的方法 1: 在minion 的配置文件夹 /etc/salt/minion.d 下面包含的配置文件 grains.conf 2: 在salt的安装目录中建立grains 文件 ...
- 递归目录的shell脚本
#! /bin/sh # 在其他目录运行时一定要加上这样的语句 # 尤其是配置在crontab里自动运行 cd `` #定义数据别名 alias statdb="/usr/local/mys ...
- 转:Java反射教程
原文来自于:http://www.importnew.com/9078.html 什么是反射?反射有什么用处? 1. 什么是反射? “反射(Reflection)能够让运行于JVM中的程序检测和修改运 ...
- SolrCloud 5.2.1 installation and configuration
虽然不是很有技术含量的事情,主要依靠的是阅读能力,然而知识的东西还是记录一下,以备后继待查. 环境相关 1. Server:h1,h2,h3 2. OS RHEL 6.2 3. Zookeeper 3 ...