首先我们系统中要用到JSON解析,所以要引入这六个架包。饱饱
不引入这六个架包的话,使用JSONObject.fromObject 和 SONObject.toBean会报错,而且有的时候编译器不会告诉你是架包缺少的错误哦!
JSONObject json = JSONObject.fromObject(String);//java转json
JSONObject.toBean()//json转java
栗子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());
}
}
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());
}
}
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;
}
}