JSON,字符串,MAP转换

package com.tree.autotest.testcase.IAuditBillDetailService;

import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import com.datatrees.basisdata.bankbill.service.IAuditBillDetailService;import com.fasterxml.jackson.databind.ObjectMapper;import com.google.common.collect.Maps;import com.tree.autotest.BaseJunit4Test;import com.tree.autotest.commons.CommonUtil_2;import com.tree.autotest.commons.TestCase;import com.tree.autotest.jdbc.DBIdConstant;import com.tree.autotest.jdbc.ManagedSqlUtil;import org.junit.After;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;import java.sql.ResultSet;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Map;

/** * Created by lyh on 17/2/23. */public class Test06 extends BaseJunit4Test{    @Autowired    private IAuditBillDetailService iAuditBillDetailService;

private boolean handleOk;    private String insertSql1,insertSql2,insertSql3,insertSql4;    private static String excel;    private static Map allCases;    private Integer testuser;    private Integer billid;    private String spare;

private Map<Short, String> bankMap = Maps.newHashMap();

@PostConstruct    public void init() {        bankMap.put((short)1, "招商银行");        bankMap.put((short)2, "广发银行");        bankMap.put((short)3, "光大银行");        bankMap.put((short)4, "华夏银行");        bankMap.put((short)5, "建设银行");        bankMap.put((short)6, "民生银行");        bankMap.put((short)7, "农业银行");        bankMap.put((short)8, "浦发银行");        bankMap.put((short)9, "兴业银行");        bankMap.put((short)10, "中国银行");        bankMap.put((short)11, "中信银行");        bankMap.put((short)12, "工商银行");        bankMap.put((short)13, "交通银行");        bankMap.put((short)14, "平安银行");        bankMap.put((short)15, "邮政储蓄银行");        bankMap.put((short)16, "广州农商银行");        bankMap.put((short)17, "浙江农信");        bankMap.put((short)18, "上海银行");        bankMap.put((short)19, "广州银行");        bankMap.put((short)20, "北京银行");        bankMap.put((short)21, "南京银行");        bankMap.put((short)22, "徽商银行");        bankMap.put((short)23, "天津银行");        bankMap.put((short)24, "苏州银行");        bankMap.put((short)25, "江苏银行");        bankMap.put((short)26, "宁波银行");        bankMap.put((short)27, "杭州银行");        bankMap.put((short)28, "重庆银行");        bankMap.put((short)29, "长沙银行");        bankMap.put((short)30, "民泰银行");        bankMap.put((short)31, "台州银行");        bankMap.put((short)32, "浙商银行");    }

@Before    public void setUp() throws SQLException {        excel = "src/main/resources/case/IAuditBillDetailService/TestT06.xlsx";        allCases = CommonUtil_2.getAllCases(excel);    }

@Test    public void test_case_1() throws Exception {

//获取用户ID        TestCase testcase = CommonUtil_2.getTestCaseBean(allCases, "case1", "case1-1");        testuser = Integer.valueOf(testcase.param1);        billid = Integer.valueOf(testcase.param2);        spare = testcase.param3;

//获取SQL        insertSql1 = testcase.stepSql;        insertSql2 = CommonUtil_2.getTestCaseBean(allCases, "case1", "case1-2").stepSql;        insertSql3 = CommonUtil_2.getTestCaseBean(allCases, "case1", "case1-3").stepSql;        insertSql4 = CommonUtil_2.getTestCaseBean(allCases, "case1", "case1-4").stepSql;

//删除,添加数据        handleOk &= deleteData(testuser,spare);        handleOk &= ManagedSqlUtil.insertBySql(insertSql1, DBIdConstant.MySql_Basisdata_ID);        handleOk &= ManagedSqlUtil.insertBySql(insertSql2,DBIdConstant.MySql_Basisdata_ID);        handleOk &= ManagedSqlUtil.insertBySql(insertSql3,DBIdConstant.MySql_Basisdata_ID);        handleOk &= ManagedSqlUtil.insertBySql(insertSql4,DBIdConstant.MySql_Basisdata_ID);        //更新数据        handleOk &=updateBillId(testuser,spare);//        Date date =DateTime.now().toDate();//        DateTime deadline = new DateTime(date).plusMonths(-18);//        DateTime specialDeadline = deadline.dayOfMonth().withMinimumValue();

//        System.out.println("DateUtils.getDateTimeNow().toDate()" + specialDeadline);

//预期值列表        List<BankBillDetail_test> excepted_detaillist = get_excepted_details(testuser);

//++++++++++++++实际值+++++++++++++        String actual_value =                iAuditBillDetailService.T06(testuser);        System.out.println("actual_value值:" + actual_value);        //字符串解析        String summary = JSON.parseObject(actual_value).getString("Summary");        String Detail = JSON.parseObject(actual_value).getString("Detail");        JSONArray list =JSON.parseArray(Detail);        //断言        Assert.assertEquals("List", summary);        Assert.assertNotNull(list);        for(int i =0;i<list.size();i++) {

    String st = list.get(i).toString();//list单个元素转换为字符串            ObjectMapper objectMapper = new ObjectMapper();            Map map = objectMapper.readValue(st, Map.class);//将字符串转换为Map.            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");            Date date=sdf.parse(map.get("PostDate").toString());//将字符串转换为Date类型            Assert.assertEquals(bankMap.get(excepted_detaillist.get(i).getBankId()), map.get("BankName"));            Assert.assertEquals(excepted_detaillist.get(i).getCardNo(), map.get("CardNo"));            Assert.assertEquals(excepted_detaillist.get(i).getTransTypeLevel1(), map.get("Type"));            Assert.assertEquals(excepted_detaillist.get(i).getDescription(), map.get("Description"));            Assert.assertEquals(excepted_detaillist.get(i).getPostDate().getTime(),date.getTime());        }

}

@After    public void tearDown() throws SQLException {        handleOk &= deleteData(testuser,spare);    }

//定义数据清理    public boolean deleteData(Integer userid,String spare) {        String deleteSql1 = "delete from t_bill where userid="+userid+" ;";        String deleteSql2 = "delete from t_bill_detail where spare1=‘"+spare+"‘;";        String deleteSql3 = "delete from t_bill_extrainfo where userid = "+userid+";";        String deleteSql4 = "delete from t_bill_installment_summary where spare1=‘"+spare+"‘ ;";        handleOk &= ManagedSqlUtil.deleteBySql(deleteSql1, DBIdConstant.MySql_Basisdata_ID);        handleOk &= ManagedSqlUtil.deleteBySql(deleteSql2, DBIdConstant.MySql_Basisdata_ID);        handleOk &= ManagedSqlUtil.deleteBySql(deleteSql3, DBIdConstant.MySql_Basisdata_ID);        handleOk &= ManagedSqlUtil.deleteBySql(deleteSql4, DBIdConstant.MySql_Basisdata_ID);        return handleOk;    }

public Boolean updateBillId(Integer userId,String Spare) throws SQLException{        String searchSql="select Id from t_bill where userid="+userId+";";        ResultSet rs =ManagedSqlUtil.selectBySql(searchSql,DBIdConstant.MySql_Basisdata_ID);        List<Integer> list = new ArrayList<Integer>();        while(rs.next()){            list.add(rs.getInt(1));        }        //更新t_bill_detail        String updateSql1="update t_bill_detail set billid="+list.get(0)+" where spare1=‘"+Spare+"‘;";        handleOk &= ManagedSqlUtil.updateBySql(updateSql1,DBIdConstant.MySql_Basisdata_ID);        //更新t_bill_extrainfo        String updateSql2="update t_bill_extrainfo set billid="+list.get(0)+" where userid="+userId+";";        handleOk &= ManagedSqlUtil.updateBySql(updateSql2, DBIdConstant.MySql_Basisdata_ID);        //更新t_bill_installment_summary        String updateSql3="update t_bill_installment_summary set billid="+list.get(0)+" where spare1=‘"+Spare+"‘;";        handleOk &= ManagedSqlUtil.updateBySql(updateSql3, DBIdConstant.MySql_Basisdata_ID);        return handleOk;    }    public List<BankBillDetail_test> get_excepted_details(Integer userid) throws SQLException {        String searchSql = "select BankId,CardNo,Level1TransType,Description,AmountMoney,PostDate from t_bill_detail where level1TransType=‘Level1_Comsumption‘ and TransactionTypeId =8 and billid in (select id from t_bill where userid="+userid+");";        ResultSet rs =ManagedSqlUtil.selectBySql(searchSql,DBIdConstant.MySql_Basisdata_ID);        List<BankBillDetail_test> list = new ArrayList<BankBillDetail_test>();        while(rs.next()){            BankBillDetail_test bankBillDetail = new BankBillDetail_test();            bankBillDetail.setBankId(rs.getShort("BankId"));            bankBillDetail.setCardNo(rs.getString("CardNo"));            bankBillDetail.setTransTypeLevel1(rs.getString("Level1TransType"));            bankBillDetail.setDescription(rs.getString("Description"));            bankBillDetail.setAmountManey(rs.getDouble("AmountMoney"));            bankBillDetail.setPostDate(rs.getDate("PostDate"));            list.add(bankBillDetail);        }        return list;    }}
时间: 2024-12-10 13:34:09

JSON,字符串,MAP转换的相关文章

JavaScript对象与JSON字符串互相转换详解

JSON是目前互联网数据传输所采用的的主要格式,很多编程语言都有针对JSON的解析器和序列化器,在web前台领域,js对象与JSON字符串的互相转换需求也较为常见. 此文主要讲解js对象和JSON字符串互相转换的方法和一些细节 首先如果需要在开发中使用JSON传输及相应转换需要在html中引入json.js或者json2.js,下载地址:https://github.com/douglascrockford/JSON-js 1.js对象转JSON字符串(序列化) var jsonText = J

C# 对象与JSON字符串互相转换的几种方式

出自:http://blog.csdn.net/testcs_dn/article/details/78150046?locationNum=6&fps=1 目录(?)[-] 方法一DoNet20 需要借助于NewtonsoftJsondll 方法二DataContractJsonSerializer 方法三JavaScriptSerializer 功能对比 性能对比 JSON转C实体类 相关参考 c json数据解析将字符串json格式数据转换成对象 Jsonnet实现方便的Json转Cdyn

C结构体与Json字符串自动转换

我们知道在Java中利用Gson这个包可以很方便地将Object和Json进行自动转换(串行化/反串行化).其原理是利用了Java的反射机制. 最近我的项目中需要将C结构体与Json自动互转.项目背景其实是一个类似protobuf的json协议生成与解析,协议很多,所以我们肯定是希望从一个C结构体的对象自动转成Json,不然协议一条一条自己写,手筋要累断. 要实现这个需求,首先就要给C结构体加入反射机制.我的处理方法是让用户在定义结构体时,额外再定义一个metainfo,用于保存这个结构体的反射

c++ JsonCpp Parse对Json字符串解析转换判断的补充 Json格式验证

最近在使用JsonCpp的时候,需要判断当前字符串是否为正确的Json格式,但是Jsoncpp对字符串进行认为是正确的json数据,导致获取的时候出错 添加一个验证的方法,在转换之前,提前验证数据是否正确,正确之后才能进行转换 1 bool IsJsonIllegal(const char *jsoncontent) 2 { 3 std::stack<char> jsonstr; 4 const char *p = jsoncontent; 5 char startChar = jsoncon

ajax请求(二),后台返回的JSon字符串的转换

ajax请求,json的转换 1 $.ajax({ 2 url : "../folder/isExistAddFolder.do?t="+new Date(), 3 type : 'POST', 4 data:{"foldername":foldername, 5 "parentid":parentid, 6 "foldertype":foldertype 7 }, 8 success : function(result){

Java读取Excel转换成JSON字符串进而转换成Java对象

Jar包 Excel数据 Java代码 package ExcelToJSON; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import java.io.File; public class Excel { public static void main(String[] args) { Sheet she

JSON字符串转换为Map (转)

本文是利用阿里巴巴封装的FastJSON来转换json字符串的.例子如下: package com.zkn.newlearn.json; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import java.util.Map; /** * JSON字符串自动转换 * Created by zkn on 2016/8/22. */ public class JsonToMapTest01 { pu

fastjson将json字符串转化成map的五种方法

fastjson将json字符串转化成map的五种方法 复制代码 1 package com.zkn.newlearn.json; 2 3 import com.alibaba.fastjson.JSON; 4 import com.alibaba.fastjson.JSONObject; 5 import java.util.Map; 6 7 /** 8 * JSON字符串自动转换 9 * 10 */ 11 public class JsonToMapTest01 { 12 13 public

JSON字符串转换为Map

转自:https://blog.csdn.net/zknxx/article/details/52281220 本文是利用阿里巴巴封装的FastJSON来转换json字符串的.例子如下: 1 import com.alibaba.fastjson.JSON; 2 import com.alibaba.fastjson.JSONObject; 3 import java.util.Map; 4 5 /** 6 * JSON字符串自动转换 7 * Created by zkn on 2016/8/2

Gson转换JSON字符串和Java对象

最近在Web开发中,用到Json和Ajax传数据,需要实现对象和Json字符串的转换,尝试了多种方法和类库,发现还是Gson比较好用. Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,反之亦可. jar包下载地址:http://code.google.com/p/google-gson/downloads/list: 不过Goole有时访问不了,可以用这个地址:http://mvnre