Json-lib 进行java与json字符串转换之二
二、list和json字符串的互转
list--》》json字符串
public static void listToJSON(){
Student stu=new Student();
stu.setName("JSON");
stu.setAge("23");
stu.setAddress("北京市海淀区");
List<Student> lists=new ArrayList<Student>();
lists.add(stu);
//1、使用JSONObject
//JSONObject listObject=JSONObject.fromObject(lists);
//2、使用JSONArray
JSONArray listArray=JSONArray.fromObject(lists);
//System.out.println("listObject:"+listObject.toString());
System.out.println("listArray:"+listArray.toString());
}
我把使用JSONObject的方式给注掉了,我们先看注释之前的结果,
Exception in thread "main" net.sf.json.JSONException: 'object' is an array. Use JSONArray instead
告诉我说有一个异常,通过查看源码发现,在使用fromObject方法的时候会先进行参数类型的判断,这里就告诉我们,传入的参数是一个array类型,因为使用的ArrayList,再来看,注释之后的结果,
listArray:[{"address":"北京市海淀区","age":"23","name":"JSON"}]
这样结果是正常的。
json字符串--》》list
从上面的例子可以看出list的对象只能转化为数组对象的格式,那么我们看下面的字符串到list的转化,
public static void jsonToList(){
String arrayStr="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"}]";
//转化为list
List<Student> list2=(List<Student>)JSONArray.toList(JSONArray.fromObject(arrayStr), Student.class);
for (Student stu : list2) {
System.out.println(stu);
}
//转化为数组
Student[] ss =(Student[])JSONArray.toArray(JSONArray.fromObject(arrayStr),Student.class);
for (Student student : ss) {
System.out.println(student);
}
}
打印结果,
Student [name=JSON, age=24, address=北京市西城区]
Student [name=JSON, age=24, address=北京市西城区]
由于字符串的格式为带有“[]”的格式,所以这里选择JSONArray这个对象,它有toArray、toList方法可供使用,前者转化为 java中的数组,或者转化为java中的list,由于这里有实体类进行对应,所以在使用时指定了泛型的类型(Student.class),这样就可 以得到转化后的对象。
三、map和json字符串的互转
map--》》json字符串
public static void mapToJSON(){
Student stu=new Student();
stu.setName("JSON");
stu.setAge("23");
stu.setAddress("中国上海");
Map<String,Student> map=new HashMap<String,Student>();
map.put("first", stu);
//1、JSONObject
JSONObject mapObject=JSONObject.fromObject(map);
System.out.println("mapObject"+mapObject.toString());
//2、JSONArray
JSONArray mapArray=JSONArray.fromObject(map);
System.out.println("mapArray:"+mapArray.toString());
}
打印结果,
mapObject{"first":{"address":"中国上海","age":"23","name":"JSON"}}
mapArray:[{"first":{"address":"中国上海","age":"23","name":"JSON"}}]
上面打印了两种形式。
json字符串--》》map
JSON字符串不能直接转化为map对象,要想取得map中的键对应的值需要别的方式,
public static void jsonToMap(){
String strObject="{\"first\":{\"address\":\"中国上海\",\"age\":\"23\",\"name\":\"JSON\"}}";
//JSONObject
JSONObject jsonObject=JSONObject.fromObject(strObject);
Map map=new HashMap();
map.put("first", Student.class);
//使用了toBean方法,需要三个参数
MyBean my=(MyBean)JSONObject.toBean(jsonObject, MyBean.class, map);
System.out.println(my.getFirst());
}
打印结果,
Student [name=JSON, age=23, address=中国上海]
下面是MyBean的代码,
package com.cn.study.day4;
import java.util.Map;
import com.cn.study.day3.Student;
public class MyBean {
private Student first;
public Student getFirst() {
return first;
}
public void setFirst(Student first) {
this.first = first;
}
}
使用toBean()方法是传入了三个参数,第一个是JSONObject对象,第二个是MyBean.class,第三个是一个Map对象。通过 MyBean可以知道此类中要有一个first的属性,且其类型为Student,要和map中的键和值类型对应,即,first对应键 first类型对应值的类型。
以上所述是小编给大家介绍的JSON字符串与java对象的相互转换实例详解,希望对大家有所帮助!
原文链接:http://www.cnblogs.com/teach/archive/2016/08/20/5791029.html
Json-lib 进行java与json字符串转换之二的更多相关文章
- Java将ip字符串转换成整数的代码
下面代码是关于Java将ip字符串转换成整数的代码,希望对各位有较大用途. public class IpUtil { public static int Ip2Int(String strIp){ ...
- Java:Json与其他Java对象集合的转换
一.引入的jar包 json-lib-2.4-jdk15.jar 二.Json字符串转换为其他对象 1.对象==>json字符串 2.list和Map集合==>json字符串 3.Map集 ...
- Java序列化对象-字符串转换
package com.test; import com.alibaba.fastjson.JSON; import org.junit.Test; import java.io.ByteArrayI ...
- js处理json数据,java处理json数据
一.js处理json数据 处理办法之一是把本机json数据或远程返回json数据用eval函数,使之变成DOM对象. 例如: var people = { "programmers" ...
- Java算法练习——字符串转换整数 (atoi)
题目链接 题目描述 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负 ...
- Java:数值-字符串转换(String转Double)
String ss = "3.141592653"; double value = Double.valueOf(ss.toString());
- Java CST格式字符串转换成Date类型的数据
Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse("We ...
- JS 将对象转换成字符 字符串转换成json对象
//js对象 var user = { "name": "张学友", "address": "中国香港" }; //将对 ...
- Jmeter_Beanshell_使用Java处理JSON块
版权声明:本文为博主原创文章,转载请注明出处. [环境] ①Jmeter版本:3.2,JDK:1.8 ②前置条件:将json.jar包置于..\apache-jmeter-3.2\lib\下,并将该j ...
随机推荐
- BZOJ3669 [Noi2014]魔法森林(SPFA+动态加边)
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- hdu 5147 Sequence II 树状数组
Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- spring的事物回滚
//默认spring只能在捕获到runtimeException时才会回滚, throw new RuntimeException("出现runtime异常"); } catch ...
- numpy array转置与两个array合并
我们知道,用 .T 或者 .transpose() 都可以将一个矩阵进行转置. 但是一维数组转置的时候有个坑,光transpose没有用,需要指定shape参数, 在array中,当维数>=2, ...
- Live disk migration with libvirt blockcopy
nova采用 libvirt blockcopy(python API virDomainBlockRebase)来做live snapshot. Create the base image: $ ...
- nginx + tomcat多实例
一.tomcat 配置多实例(修改两个端口:server端口,tomcat端口) 搭建之前,先确保已安装java和tomcat jdk安装:http://note.youdao.com/notes ...
- 表格表格中获取不到button选择器
今天做一个表单提交,怎么也拿不到button的选择器,不管用$(“#btn_update”)还会getElementById("btn_update"),浏览器也是谷歌没问题,后来 ...
- 抓包tcpdump
tcpdump是基于Unix系统的命令行式的数据包嗅探工具 tcpdump 可以监测真机或者模拟器 2g/3g/wifi下数据流动,前提手机必须root过. fillder也可以检测客户端数据包 ...
- 51nod 1105 二分好题
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1105 1105 第K大的数 基准时间限制:1 秒 空间限制:131072 ...
- Asp.net 使用 Jsonp
简介 由于JavaScript的安全机制,ajax不支持跨域调用.所以出现了jsonp. 实现 服务器 public string Jsonp(string name) { string result ...