JSON与JAVA数据的转换。JSONObject.fromObject()和JSONObject.toBean()

六个架包(666)

首先我们系统中要用到JSON解析,所以要引入这六个架包。饱饱

不引入这六个架包的话,使用JSONObject.fromObject  和 SONObject.toBean会报错,而且有的时候编译器不会告诉你是架包缺少的错误哦!

JSON与JAVA数据的转换。JSONObject.fromObject()和JSONObject.toBean()_第1张图片

JSON 对象  与  JAVA对象的转换

  1. JSONObject   json = JSONObject.fromObject(String);//java转json

  2. JSONObject.toBean()//json转java

(1)java对象转json对象。

                ——HashMap转为JSON对象:JSONObject.fromObject()

          json对象又再次转换成java对象

                 ——JSON转为Map:JSON强制转换

栗子1

public class JSONTest
{
	public static void main(String[] args)
	{
		HashMap map=new HashMap();
		map.put("A", "B");//map={A=B}
		JSONObject json = JSONObject.fromObject(map);//json={"A":"B"}
		System.out.println(json.toString());//{"A":"B"}

        //再将json强转成map
		Map map2=(Map)json;//{"A":"B"}
		System.out.println(map2.toString());
	}
}

忽然发现两件事:

一件事:HashMap中key值相同的话,前一个会被后一个覆盖。(原谅我太渣,现在才知道)

第二件事:相比于有道笔记,饱饱更爱写CSDN啊!

public class JSONTest
{
	public static void main(String[] args)
	{
		HashMap map=new HashMap();
		map.put("A", "B");//map={A=B}
		map.put("A", "C");//map={A=C}
		JSONObject json = JSONObject.fromObject(map);//json={"A":"C"}
		System.out.println(json.toString());//{"A":"C"}

        //再将json强转成map
		Map map2=(Map)json;//{"A":"C"}
		System.out.println(map2.toString());
	}
}

栗子2

public class JSONTest
{
	public static void main(String[] args)
	{
		HashMap map=new HashMap();
		map.put("A", "B");//map={A=B}
		map.put("C", "D");//map={A=B, C=D}
		JSONObject json = JSONObject.fromObject(map);//json={"A":"B","C":"D"}
		System.out.println(json.toString());

        //再将json强转成map
		Map map2=(Map)json;//{"A":"B","C":"D"}
		System.out.println(map2.toString());
	}
}

(2)java对象转json对象。

                ——String转为JSON对象:JSONObject.fromObject()

          json对象又再次转换成java对象

                 ——JSON转为Map:JSON强制转换

mapString =
{
	"errmsg":"ok",
	"ErrorCode":0,
	"messageId":"f5d4566c4c833152b3feb42a8092ee6c",
	"invaliduser":"",
	"forbiddenUserId":"",
	"resJson":{"errmsg":"ok","ErrorCode":0,"messageId":"f5d4566c4c833152b3feb42a8092ee6c","invaliduser":"","forbiddenUserId":""}
}
public class JSONTest
{
	public static void main(String[] args)
	{
		//将string类型的map数据转换为json的数据格式,在将json的数据格式的转换成java的    Map的数据类型
		String mapString = "{\"errmsg\":\"ok\",\"ErrorCode\":0,\"messageId\":\"f5d4566c4c833152b3feb42a8092ee6c\",\"invaliduser\":\"\",\"forbiddenUserId\":\"\",\"resJson\":{\"errmsg\":\"ok\",\"ErrorCode\":0,\"messageId\":\"f5d4566c4c833152b3feb42a8092ee6c\",\"invaliduser\":\"\",\"forbiddenUserId\":\"\"}}";
		//mapString={"errmsg":"ok","ErrorCode":0,"messageId":"f5d4566c4c833152b3feb42a8092ee6c","invaliduser":"","forbiddenUserId":"","resJson":{"errmsg":"ok","ErrorCode":0,"messageId":"f5d4566c4c833152b3feb42a8092ee6c","invaliduser":"","forbiddenUserId":""}}
		JSONObject json = JSONObject.fromObject(mapString);
		//json	   ={"errmsg":"ok","ErrorCode":0,"messageId":"f5d4566c4c833152b3feb42a8092ee6c","invaliduser":"","forbiddenUserId":"","resJson":{"errmsg":"ok","ErrorCode":0,"messageId":"f5d4566c4c833152b3feb42a8092ee6c","invaliduser":"","forbiddenUserId":""}}
		Map map = (Map)json;
		//map      ={"errmsg":"ok","ErrorCode":0,"messageId":"f5d4566c4c833152b3feb42a8092ee6c","invaliduser":"","forbiddenUserId":"","resJson":{"errmsg":"ok","ErrorCode":0,"messageId":"f5d4566c4c833152b3feb42a8092ee6c","invaliduser":"","forbiddenUserId":""}}
		System.out.println("-----map:---"+map.toString());
	}
}

(3)java对象转json对象。

                 ——String转为JSON对象:JSONObject.fromObject()

          json对象又再次转换成java对象

                  ——JSON转为Student对象:JSONObject.toBean()

public class JSONTest
{
	public static void main(String[] args)
	{
		String mapString = "{id:'211415034',name:'徐妍',age:'24'}";
		Student student = new Student();
		JSONObject json = JSONObject.fromObject(mapString);//json={"id":"211415034","name":"徐妍","age":"24"}
		student = (Student)JSONObject.toBean(json, Student.class);//曾经的学号:211415034    姓名:徐妍    年龄:24
		//JSONObject.toBean() 方法意为:从JSONObject 对象转换为 JAVA 对象。
		System.out.println(student);
	}
}
public class Student
{
	private int id;
	private String name;
	private int age;
	public int getId()
	{
		return id;
	}
	public void setId(int id)
	{
		this.id = id;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public int getAge()
	{
		return age;
	}
	public void setAge(int age)
	{
		this.age = age;
	}	
	public String toString()
	{
		return "曾经的学号:"+this.id+"    姓名:"+this.name+"    年龄:"+this.age;
	}
}

 

你可能感兴趣的:(Java——SWT)