Json、JavaBean、String等互转

本文介绍简单的Json、JavaBean、String互换(下文JavaBean简称Object对象,这里不是很严谨)

转换关系如下:

其中String和javaBean之间的转换可以通过“中间态”Json来完成。

先上代码,再补充解释

 

实体类

package com.blue.yanxishe.controller.app.market;
public class Student {
	private String name;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}	
}

 实体类

package com.blue.yanxishe.controller.app.market;
public class MyStudent {
	private Student myStudent ;

	public Student getMyStudent() {
		return myStudent;
	}

	public void setMyStudent(Student myStudent) {
		this.myStudent = myStudent;
	}	
}  

 测试示例

 

package com.blue.yanxishe.controller.app.market;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.JsonObject;

import net.sf.json.JSONObject;

public class JsonStudy {
	static String stuStr= "{\"name\":\"小明\",\"age\":18}"; 
	static JSONObject stu_json = JSONObject.fromObject(stuStr);
	public static void StringToJson(){
		 JSONObject stujson = JSONObject.fromObject(stuStr);
		System.out.println(stujson);//输出:{"name":"小明","age":18}
	}	
	public static void StringToObject(){
		JSONObject stujson = JSONObject.fromObject(stuStr);
		Student stuObject = (Student)JSONObject.toBean(stujson, Student.class);
		System.out.println("我的名字叫"+stuObject.getName()+",今年"+stuObject.getAge()+"岁!");	//输出:我的名字叫小明,今年18岁!
	}	
	public static void JsonToObject(){
		JSONObject stujson = JSONObject.fromObject(stuStr);
		Object stuObject = JSONObject.toBean(stujson, Student.class);
		Student stuEntity = (Student)stuObject;
		System.out.println("我的名字叫"+stuEntity.getName()+",今年"+stuEntity.getAge()+"岁!");	//	输出:我的名字叫小明,今年18岁!
	}
	public static void JsonToString(){
		String stuString = stu_json.toString();
		System.out.println(stuString);	//	输出:{"name":"小明","age":18}
	}
	public static void ObjectToJson(){
		Student stuObject = new Student();
		stuObject.setAge(new Integer(18));
		stuObject.setName("冬冬");
		JSONObject stuJson = JSONObject.fromObject(stuObject);
		System.out.println(stuJson);//{"name":"冬冬","age":18}
	}
	public static void ObjectToString(){
		Student stuObject = new Student();
		stuObject.setAge(new Integer(18));
		stuObject.setName("冬冬");
		JSONObject stuJson = JSONObject.fromObject(stuObject);
		String stuString = stuJson.toString();
		System.out.println(stuString);//输出:{"name":"冬冬","age":18}
	}
	public static void main(String[] args) {
		StringToJson();
		JsonToObject();
		JsonToObject();
		JsonToString();
		ObjectToJson();	
		ObjectToString();
	}
}

 

2019-02-27补充:

对于复杂的嵌套的实体类,可能会包报net.sf.ezmorph.bean.MorphDynaBean转换异常 

原因:由于需要转换的对象中存在其他对象的引用,并且在转换的时候没有指定对应的类,此时json会使用默认的bean进行动态转换MorphDynaBean,但是在使用的时候就会出现类型转换异常,代码参考如下

public class A implements Serializable{

    private static final long serialVersionUID = 1L;

    private List<B> b;
    private List<B> c;
    //get、set方法 省略
}

解决方法,在转换之前设置类信息,即显示说明实体类内部嵌套的类的类型

Map<String,Class<?>> classMap = new HashMap<String,Class<?>>();
classMap.put("b", B.class);
classMap.put("c", C.class);
JSONObject.toBean(jsonobject, A.class,classMap);

 

 

 

参考文献

 1-https://blog.csdn.net/sld880311/article/details/72841834

posted on 2017-09-24 16:02  我不吃番茄  阅读(3018)  评论(0编辑  收藏  举报