1.工程

2.代码:

JsonUtil.java

package com.my.json;

import java.util.ArrayList;
import java.util.List; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class JsonUtil { /**
* 从一个JSON 对象字符格式中得到一个java对象
*
* @param jsonString
* json字符串
*
* @param pojoCalss
* java对象的class
*
* @return
*/ public static Object jsonString2Object(String jsonString, Class pojoCalss) { Object pojo; JSONObject jsonObject = JSONObject.fromObject(jsonString); pojo = JSONObject.toBean(jsonObject, pojoCalss); return pojo; } /**
*
* 将java对象转换成json字符串<br>
* 支持复杂类型:Java bean 中既有属性又有list
*
* @param javaObj
*
* @return
*/ public static String object2JsonString(Object javaObj) { JSONObject json; json = JSONObject.fromObject(javaObj); return json.toString(); } /**
*
* 从json对象集合表达式中得到一个java对象列表
*
* @param jsonString
*
* @param pojoClass
*
* @return
*/ public static List jsonList2JavaList(String jsonString, Class pojoClass) { JSONArray jsonArray = JSONArray.fromObject(jsonString); JSONObject jsonObject; Object pojoValue; List list = new ArrayList(); for (int i = 0; i < jsonArray.size(); i++) { jsonObject = jsonArray.getJSONObject(i); pojoValue = JSONObject.toBean(jsonObject, pojoClass); list.add(pojoValue); } return list; } /**
*
* 从json对象集合表达式中得到一个java对象列表
*
* @param jsonString
*
* @param pojoClass
*
* @return
*/ public static String javaList2JsonList(List list) { JSONArray jsonArray = JSONArray.fromObject(list);
return jsonArray.toString();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static Map<String, Object> parseJSON2Map(String jsonStr){ 
        Map<String, Object> map = new HashMap<String, Object>(); 
        //最外层解析 
        JSONObject json = JSONObject.fromObject(jsonStr); 
        for(Object k : json.keySet()){ 
            Object v = json.get(k);  
            //如果内层还是数组的话,继续解析 
            if(v instanceof JSONArray){ 
                List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); 
                Iterator<JSONObject> it = ((JSONArray)v).iterator(); 
                while(it.hasNext()){ 
                    JSONObject json2 = it.next(); 
                    list.add(parseJSON2Map(json2.toString())); 
                
                map.put(k.toString(), list); 
            } else 
                map.put(k.toString(), v); 
            
        
        return map; 
    }

  

}

Person.java

package com.my.json;

import java.util.ArrayList;
import java.util.List; public class Person { private String name; private List<Person> list = new ArrayList<Person>(); public List<Person> getList() {
return list;
} public void setList(List<Person> list) {
this.list = list;
} @Override
public String toString() {
return "Person [name=" + name + ", num=" + num + ", sex=" + sex
+ ", age=" + age + "]";
} private int num;
private String sex;
private int age; public Person() {
// TODO Auto-generated constructor stub
} public Person(String name, int num, String sex, int age) {
super();
this.name = name;
this.num = num;
this.sex = sex;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getNum() {
return num;
} public void setNum(int num) {
this.num = num;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }

Test.java

package com.my.json;

import java.util.ArrayList;
import java.util.List;
import java.util.Map; import net.sf.json.JSONObject; public class Test {
public static void main(String[] args) { // 将一个java对象转换为Json字符串
Person p = new Person("gu1", 123, "male", 23);
String str = JsonUtil.object2JsonString(p);
System.out.println(str); // 将一个JSON字符串转化为Java对象
String s = "{\"age\":23,\"name\":\"gu1\",\"num\":123,\"sex\":\"male\"}";
Person p2 = (Person) JsonUtil.jsonString2Object(s, Person.class);
System.out.println(p2); // 将一个对象列表转化为JSON列表
List<Person> list = new ArrayList<Person>();
for (int i = 0; i < 5; i++) {
Person persion = new Person("zhang" + i, 1 + i, "man" + i, 1 + i);
list.add(persion);
}
String jsonList = JsonUtil.javaList2JsonList(list);
System.out.println(jsonList); // 将一个JSON列表转化为Java列表
String jst = "[{\"age\":1,\"list\":[],\"name\":\"zhang0\",\"num\":1,\"sex\":\"man0\"},{\"age\":2,\"list\":[],\"name\":\"zhang1\",\"num\":2,\"sex\":\"man1\"},{\"age\":3,\"list\":[],\"name\":\"zhang2\",\"num\":3,\"sex\":\"man2\"},{\"age\":4,\"list\":[],\"name\":\"zhang3\",\"num\":4,\"sex\":\"man3\"},{\"age\":5,\"list\":[],\"name\":\"zhang4\",\"num\":5,\"sex\":\"man4\"}]";
List<Person> l = (List<Person>) JsonUtil.jsonList2JavaList(jst,
Person.class);
System.out.println(l.size()); // 复杂类型转JSON:Java bean 中既有属性又有list(项目中经常见到)
List<Person> list2 = new ArrayList<Person>();
for (int i = 0; i < 10; i++) {
Person persion = new Person("zhang" + i, 1 + i, "man" + i, 1 + i);
list2.add(persion);
}
Person me = new Person("zhang", 1, "man", 1);
me.setList(list2); String str2 = JsonUtil.object2JsonString(me);
System.out.println(str2); // 复杂类型转Java:JSON 中既有属性又有list(项目中经常见到)
String ss = "{\"age\":1,\"list\":[{\"age\":1,\"list\":[],\"name\":\"zhang0\",\"num\":1,\"sex\":\"man0\"},{\"age\":2,\"list\":[],\"name\":\"zhang1\",\"num\":2,\"sex\":\"man1\"},{\"age\":3,\"list\":[],\"name\":\"zhang2\",\"num\":3,\"sex\":\"man2\"},{\"age\":4,\"list\":[],\"name\":\"zhang3\",\"num\":4,\"sex\":\"man3\"},{\"age\":5,\"list\":[],\"name\":\"zhang4\",\"num\":5,\"sex\":\"man4\"},{\"age\":6,\"list\":[],\"name\":\"zhang5\",\"num\":6,\"sex\":\"man5\"},{\"age\":7,\"list\":[],\"name\":\"zhang6\",\"num\":7,\"sex\":\"man6\"},{\"age\":8,\"list\":[],\"name\":\"zhang7\",\"num\":8,\"sex\":\"man7\"},{\"age\":9,\"list\":[],\"name\":\"zhang8\",\"num\":9,\"sex\":\"man8\"},{\"age\":10,\"list\":[],\"name\":\"zhang9\",\"num\":10,\"sex\":\"man9\"}],\"name\":\"zhang\",\"num\":1,\"sex\":\"man\"}";
Map map = (Map)JSONObject.fromObject(ss); System.out.println("------------------------------");
System.out.println(map);
String name = (String)map.get("name");
int num = (Integer)map.get("num");
String sex = (String)map.get("sex");
int age = (Integer)map.get("age");
System.out.println("name="+name);
System.out.println("num="+"num");
System.out.println("sex="+"sex");
System.out.println("age="+"age");
System.out.println("list="+map.get("list"));
//获取list字符串,再转化为Java 对象,其实是再次构建成Java bean
String listStr = map.get("list").toString();//一定要用toString,不能强转
List<Person> ll = JsonUtil.jsonList2JavaList(listStr, Person.class);
//构建新的bean:不知是否有其他方案,根据需要是否构建
Person pp = new Person(name,num,sex,age);
pp.setList(ll);
//输出bean
for (Person ps : pp.getList())
{
System.out.println(ps.getName());
} }
}

输出:

{"age":23,"list":[],"name":"gu1","num":123,"sex":"male"}
Person [name=gu1, num=123, sex=male, age=23]
[{"age":1,"list":[],"name":"zhang0","num":1,"sex":"man0"},{"age":2,"list":[],"name":"zhang1","num":2,"sex":"man1"},{"age":3,"list":[],"name":"zhang2","num":3,"sex":"man2"},{"age":4,"list":[],"name":"zhang3","num":4,"sex":"man3"},{"age":5,"list":[],"name":"zhang4","num":5,"sex":"man4"}]
5
{"age":1,"list":[{"age":1,"list":[],"name":"zhang0","num":1,"sex":"man0"},{"age":2,"list":[],"name":"zhang1","num":2,"sex":"man1"},{"age":3,"list":[],"name":"zhang2","num":3,"sex":"man2"},{"age":4,"list":[],"name":"zhang3","num":4,"sex":"man3"},{"age":5,"list":[],"name":"zhang4","num":5,"sex":"man4"},{"age":6,"list":[],"name":"zhang5","num":6,"sex":"man5"},{"age":7,"list":[],"name":"zhang6","num":7,"sex":"man6"},{"age":8,"list":[],"name":"zhang7","num":8,"sex":"man7"},{"age":9,"list":[],"name":"zhang8","num":9,"sex":"man8"},{"age":10,"list":[],"name":"zhang9","num":10,"sex":"man9"}],"name":"zhang","num":1,"sex":"man"}
------------------------------
{"age":1,"list":[{"age":1,"list":[],"name":"zhang0","num":1,"sex":"man0"},{"age":2,"list":[],"name":"zhang1","num":2,"sex":"man1"},{"age":3,"list":[],"name":"zhang2","num":3,"sex":"man2"},{"age":4,"list":[],"name":"zhang3","num":4,"sex":"man3"},{"age":5,"list":[],"name":"zhang4","num":5,"sex":"man4"},{"age":6,"list":[],"name":"zhang5","num":6,"sex":"man5"},{"age":7,"list":[],"name":"zhang6","num":7,"sex":"man6"},{"age":8,"list":[],"name":"zhang7","num":8,"sex":"man7"},{"age":9,"list":[],"name":"zhang8","num":9,"sex":"man8"},{"age":10,"list":[],"name":"zhang9","num":10,"sex":"man9"}],"name":"zhang","num":1,"sex":"man"}
name=zhang
num=num
sex=sex
age=age
list=[{"age":1,"list":[],"name":"zhang0","num":1,"sex":"man0"},{"age":2,"list":[],"name":"zhang1","num":2,"sex":"man1"},{"age":3,"list":[],"name":"zhang2","num":3,"sex":"man2"},{"age":4,"list":[],"name":"zhang3","num":4,"sex":"man3"},{"age":5,"list":[],"name":"zhang4","num":5,"sex":"man4"},{"age":6,"list":[],"name":"zhang5","num":6,"sex":"man5"},{"age":7,"list":[],"name":"zhang6","num":7,"sex":"man6"},{"age":8,"list":[],"name":"zhang7","num":8,"sex":"man7"},{"age":9,"list":[],"name":"zhang8","num":9,"sex":"man8"},{"age":10,"list":[],"name":"zhang9","num":10,"sex":"man9"}]
zhang0
zhang1
zhang2
zhang3
zhang4
zhang5
zhang6
zhang7
zhang8
zhang9

ason 和 Java 对象转化示例的更多相关文章

  1. Jason 和 Java 对象转化示例

    1.工程 2.代码: JsonUtil.java package com.my.json; import java.util.ArrayList; import java.util.List; imp ...

  2. Java对象池示例

    单例模式是限制了一个类只能有一个实例,对象池模式则是限制一个类实例的个数.对象池类就像是一个对象管理员,它以Static列表(也就是装对象的池子)的形式存存储某个实例数受限的类的实例,每一个实例还要加 ...

  3. JAVA对象转化JSON出现死循环问题

    主要是解决JSON因Hibernate映射生成的集合的转化出现的死循环问题. 这个方法很重要 public String ajaxJsonByObjectDirecdt(Object obj, Str ...

  4. [Java] 对象排序示例

    package test.collections; import java.util.ArrayList; import java.util.Collection; import java.util. ...

  5. json字符串转java对象

    今天遇到一个问题,前端ajax获取到一个javaBean对象,好多方法发ajax请求需要把这个对象再传到后端,后端再根据这个对象进行操作(之前计划传递id,但发现传递id的话,后端多个方法都需要根据i ...

  6. 【Java】详解java对象的序列化

    目录结构: contents structure [+] 序列化的含义和意义 使用对象流实现序列化 对象引用的序列化 自定义序列化 采用实现Serializable接口实现序列化 采用实现Extern ...

  7. JAVA对象序列化和反序列化学习

    JAVA序列化就是将JAVA对象转化为字节序列的过程,而JAVA反序列化就是将字节序列转化为JAVA对象的过程. 这一过程是通过JAVA虚拟机独立完成,所以一个对象序列化后可以在任意时间和任意机器上反 ...

  8. 8.RabbitMQ 消息传递Java对象

    通过消息服务器传递Java对象,Java类必须实现序列化接口,可以把Java对象转化为字节数组,从消费者或生产者传递到另外一个JVM中,一定需要两个JVM共享这个类,比如是UserInfo类.   1 ...

  9. 使用XStream注解实现Java对象与XML互相转换的代码示例

    本文记录一下使用xstream这个api的注解特性对Java对象与XML字符串相互转换的一些代码示例.    我们很多人都处理过XML文件,也有很多非常成熟的第三方开源软件.如:jdom.dom4j等 ...

随机推荐

  1. DIV+CSS设计IE6浮动产生双倍距离

    <!doctype html><html><head> <meta name="Keywords" content="" ...

  2. SecureCRT rz和sz命令不可用,安装lrzsz

    1.从网站下载 lrzsz-x.xx.xx.tar.gz2.解压文件[root@localhost src]# tar zxvf lrzsz-0.12.20.tar.gz3.安装[root@local ...

  3. matlab产生正态分布样本

    mvnrnd - Multivariate normal random numbers This MATLAB function returns an n-by-d matrix R of rando ...

  4. ci(转)

    1  从代码管理器签出源文件 2  修改代码 3  编译代码 4  遇到错误,转到2继续修改直到达到预期 5  运行单元测试,期望所有的测试绿色(通过) 6  单元测试出错,转入2 7  重构代码,按 ...

  5. 学习opengl十大网站(转载)

    [转载] 1.http://nehe.gamedev.net/这个是我觉得全世界最知名的OpenGL教程,而且有网友将其中48个教程翻译成了中文http://www.owlei.com/Dancing ...

  6. WinEdt和TeXworks编辑LaTeX文件乱码问题

    WinEdt默认使用的是系统编码,windows下可以认为是 GBK编码,而TeXworks默认使用的是UTF8编码,所以要统一这两个编码,才能保证两个文件互相打开不会乱码. 具体方法如下: 一,可以 ...

  7. ubuntu14.04 ibus pinyin wrong (ibus拼音乱拼问题)

    在ubuntu14.04版的中文输入法ibus中,有时会出现拼音乱拼的问题.不过已经有了成熟的解决方案. 具体方法如下: apt-get remove ibus-pinyin apt-get inst ...

  8. Prim求解最小生成树

    #include "ljjz.h" typedef struct edgedata /*用于保存最小生成树的边类型定义*/ { int beg,en; /*beg,en是边顶点序号 ...

  9. 转:如何理解c和c ++的复杂类型声明

    本文作者girlrong是网易广州社区的C语言版版主,这篇文章被选在精华区.很是不错,不敢独享!据说她乐于助人,虚心诚恳,颇受网友欢迎.只可惜现在已退隐江湖了.在最近学习C语言过程中,了解些前辈大牛的 ...

  10. Objective-c——UI基础开发第八天(QQ聊天界面)

    一.知识点: QQ聊天界面 双模型的使用(dataModel和frameModel) UITextField的使用 通知的使用 拉伸图片的两种方法(slicing/image对象的resizeable ...