Java:Json与其他Java对象集合的转换
一、引入的jar包
json-lib-2.4-jdk15.jar
二、Json字符串转换为其他对象
1、对象==》json字符串 2、list和Map集合==》json字符串 3、Map集合==》json字符串【具体代码如下】
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap; import com.coscon.ferightEntrust.vo.CdViewVo; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class TestCd {
public static void main(String[] args) {
collectToStr();
} private static void collectToStr(){ CdViewVo cvv=new CdViewVo();
cvv.setCntrNum("CBHU898637"); cvv.setSealNum("CBHU898637");
cvv.setCntrType("20Gp"); cvv.setObTrafficTerm("jkd");
cvv.setIbTrafficTerm("P-预付"); cvv.setPackDesc("");
cvv.setGrossWt(""); cvv.setVol("54.23"); CdViewVo cvv2=new CdViewVo();
cvv2.setCntrNum("CBHU893437"); cvv2.setSealNum("CBHU893437");
cvv2.setCntrType("40Gp"); cvv2.setObTrafficTerm("bhc");
cvv2.setIbTrafficTerm("P-预付"); cvv2.setPackDesc("");
cvv2.setGrossWt(""); cvv2.setVol("84.23");
//Java对象
JSONObject cvvo=JSONObject.fromObject(cvv);
String cvvs=cvvo.toString();
System.out.println("CdViewVo的json字符串:"+cvvs);
//map集合
Map<String, String> tm=new TreeMap<String, String>();
tm.put("a", "ddd"); tm.put("b", "ccc");
tm.put("c", "bbb"); tm.put("d", "aaa");
JSONArray array=JSONArray.fromObject(tm);
String tmstr=array.toString();
System.out.println("Map集合对应的json字符串:"+tmstr); //list集合
List<CdViewVo> list=new ArrayList<CdViewVo>();
list.add(cvv);
list.add(cvv2);
JSONArray listArr=JSONArray.fromObject(list);
String listStr=listArr.toString();
System.out.println("listStr集合对应的json字符串:"+listStr);
}
}
运行结果如下:
CdViewVo的json字符串:{"cntrNum":"CBHU898637","cntrType":"20Gp","grossWt":"","ibTrafficTerm":"P-预付","obTrafficTerm":"jkd","packDesc":"","packNum":"","sealNum":"CBHU898637","vol":"54.23"}
Map集合对应的json字符串:[{"a":"ddd","b":"ccc","c":"bbb","d":"aaa"}]
listStr集合对应的json字符串:[{"cntrNum":"CBHU898637","cntrType":"20Gp","grossWt":"","ibTrafficTerm":"P-预付","obTrafficTerm":"jkd","packDesc":"","packNum":"","sealNum":"CBHU898637","vol":"54.23"},
{"cntrNum":"CBHU893437","cntrType":"40Gp","grossWt":"","ibTrafficTerm":"P-预付","obTrafficTerm":"bhc","packDesc":"","packNum":"","sealNum":"CBHU893437","vol":"84.23"}]
三、Java对象转换为Json字符串
import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class TestCd {
public static void main(String[] args) {
jsonStrToJava();
}
private static void jsonStrToJava(){
String jsonStr="{\"cntrNum\":\"CBHU898637\",\"cntrType\":\"20Gp\",\"grossWt\":\"100\"," +
"\"ibTrafficTerm\":\"P-预付\",\"obTrafficTerm\":\"jkd\",\"packDesc\":\"12\"," +
"\"packNum\":\"\",\"sealNum\":\"CBHU898637\",\"vol\":\"54.23\"}";
JSONObject java=JSONObject.fromObject(jsonStr);
CdViewVo cvv=(CdViewVo) JSONObject.toBean(java,CdViewVo.class);
System.out.println("cntrNum: "+cvv.getCntrNum());
}
}
运行结果:
cntrNum: CBHU898637
四、集合转换为Json字符串
String str = "{\"lendperson\":\"李四\",\"lendcompany\":\"有限公司\",\"checkperson\":\"李四\",
\"lenddate\":\"2010-07-19T00:00:00\",\"lendcounts\":4,\"
passports\":[{\"passportid\":\"d\",\"name\":\"李豫川\",\"passporttype\":\"K\"},
{\"passportid\":\"K9051\",\"name\":\"李平\",\"passporttype\":\"K\"},
{\"passportid\":\"K90517\",\"name\":\"袁寒梅\",\"passporttype\":\"K\"},
{\"passportid\":\"K905199\",\"name\":\"贺明\",\"passporttype\":\"K\"}]}"; JSONObject jsonobject = JSONObject.fromObject(str);
PassportLendsEntity passportlends = null;
try {
//获取一个json数组
JSONArray array = jsonobject.getJSONArray("passports");
//将json数组 转换成 List<PassPortForLendsEntity>泛型
List<PassPortForLendsEntity> list = new ArrayList<PassPortForLendsEntity>();
for (int i = ; i < array.size(); i++) {
JSONObject object = (JSONObject)array.get(i);
PassPortForLendsEntity passport = (PassPortForLendsEntity)JSONObject.toBean(object,
PassPortForLendsEntity.class); //转换成Java对象
if(passport != null){
list.add(passport);
}
}
Java:Json与其他Java对象集合的转换的更多相关文章
- JSON字符串和JS对象之间的转换
JSON字符串和JS对象之间的转换 1 json字符串转换为js对象 1.1 标准json格式字符串转换为Js对象 JSON字符串 str JSON.parse(str) eval(str) eva ...
- java json数据转List对象的集合-----阿里巴巴插件---及原生json---JSON 与 对象 、集合 之间的转换 JSON字符串和java对象的互转【json-lib】
List<RunfastFullLess> list=(List<RunfastFullLess>)JSONArray.parseObject(activity.getFull ...
- 【java/Json】用Java对象构建Json语法树
本文后续:https://www.cnblogs.com/xiandedanteng/p/11973129.html 编译第一步:将文本解析成Java对象构成的语法树 第二步:将语法树输出整形好的Js ...
- Java Json字符串转Java对象遇到属性大小写的问题解决
String objectStr="{\"namE\":\"JSON\",\"age\":\"24\",\&q ...
- java中拼接两个对象集合
目标: 根据两个list中每条记录的某个属性是否相同来拼接. 1.首先定义一个字符串 String str = "[{\"ITEMID\":2,\"ITEMN ...
- Scala和Java的List集合互相转换
import java.util import scala.collection.mutable /** * 集合互相转换 */ object ScalaToJava { def main(args: ...
- JAVA中java.util.Date、java.sql.Timestamp和String之间的互相转换
java.util.Date与的String互转 java.util.Date---->String /** * 将java.util.Date对象转化为String字符串 * @param d ...
- java json与对象或者集合互转
package open_exe; public class User { private int id; private String name; private String gender; pu ...
- Json——使用Json jar包实现Json字符串与Java对象或集合之间的互相转换
总结一下利用Json相关jar包实现Java对象和集合与Json字符串之间的互相转换: 1.创建的User类: package com.ghj.packageofdomain; public clas ...
随机推荐
- jquery+NHibernate3.3.3+MVC的分页效果
目录结构 先设计数据表结构 DROP SEQUENCE BDLLY_2V.SEQ_CUSTOMER; CREATE SEQUENCE BDLLY_2V.SEQ_CUSTOMER START MAXVA ...
- 页面与母版页面的asp:ContentPlaceHolder不匹配
问题描述:The page has one or more <asp:Content> controls that do not correspond with <asp:Conte ...
- wireshark不仅仅是用来抓包分析网络的;
凡是不找借口,不排弄推诿理由,提高自我,尽量人事; AIX smit(system manager interface tool); formerly alike; derivatives dpkg ...
- 完美转换MySQL的字符集 Mysql 数据的导入导出,Mysql 4.1导入到4.0
MySQL从4.1版本开始才提出字符集的概念,所以对于MySQL4.0及其以下的版本,他们的字符集都是Latin1的,所以有时候需要对mysql的字符集进行一下转换,MySQL版本的升级.降级,特别是 ...
- sublime 汉化及注册
首先安装 package control https://packagecontrol.io/installation 网站上面有详细说明 安装以后快捷键 ctrl +shift+p 输入ip ...
- Linux后门入侵检测工具,附bash漏洞解决方法[转载]
转自:http://blog.jobbole.com/77663/ 官网 ClamAV杀毒软件介绍 ClamAV是一个在命令行下查毒软件,因为它不将杀毒作为主要功能,默认只能查出您计算机内的病毒,但是 ...
- 基于windows的ngnix基础使用
前言 今天组长一大早心血来潮的跟我说,我希望我们小组电脑做web站点的服务器集群,你搞一搞,就用ngnix吧. 君要臣死,臣不得不死.顺便写个文章做个笔记. 简介 Nginx 是一个高性能的HTTP和 ...
- [TypeScript] Installing TypeScript and Running the TypeScript Compiler (tsc)
This lesson shows you how to install TypeScript and run the TypeScript compiler against a .ts file f ...
- tcpreplay安装使用经验
tcpreplay安装使用经验 #Author: ypguo#Data: 2010.4.23#Version: 1.2 增加了修改VLAN tag内容. 1.1 ...
- Linux下安装GAMS建模优化工具
1.下载GAMS wget http://d37drm4t2jghv5.cloudfront.net/distributions/24.5.6/linux/linux_x64_64_sfx.exe 2 ...