测试类:
package com.jpa.demo.类型转换包;
import com.fasterxml.jackson.databind.util.JSONPObject;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.json.JsonObject;
public class Test001 {
public static void main(String[] args) {
Student student=new Student();
student.setName("一号");
student.setAddress("一号地址");
student.setAge("一号年龄");
// 使用JSONObject.fromObject
JSONObject jsonObject = JSONObject.fromObject(student);
// 使用JSONArray.fromObject
JSONArray jsonArray = JSONArray.fromObject(student);
String strJson=jsonObject.toString();
String strArray=jsonArray.toString();
// java对象转化为JSON字符串,只是转化后的结构不同。
System.out.println(strJson+"Json 字符串格式");
System.out.println(strArray+"Json 对象字符串格式");
System.out.println( "========定义两种不同格式的字符串==========");
//定义两种不同格式的字符串
String objectStr="{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"我是一号\"}";
String arrayStr="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"我是一号\"}]";
//1、使用JSONObject
JSONObject jsonObject1=JSONObject.fromObject(objectStr);
Student stu=(Student)JSONObject.toBean(jsonObject1, Student.class);
JSONArray jsonArray1=JSONArray.fromObject(arrayStr);
Object o=jsonArray1.get(0);
JSONObject jsonObject2=JSONObject.fromObject(o);
Student stu2=(Student)JSONObject.toBean(jsonObject2, Student.class);
// ,使用JSONObject可以轻松的把JSON格式的字符串转化为java对象,但是使用JSONArray就没那么容易了,
// 因为它有“[]”符号,所以我们这里在获得了JSONArray的对象之后,取其第一个元素即我们需要的一个student的变形,然后使用JSONObject轻松获得。
System.out.println("stu:"+stu);
System.out.println("stu2:"+stu2);
// List和Json字符串的互转 list的对象 转化为数组对象
System.out.println( "======== List和Json字符串的互转=========");
Student student1=new Student();
student1.setName("JSON");
student1.setAge("23");
student1.setAddress("北京市海淀区");
List<Student> lists=new ArrayList<Student>();
lists.add(student1);
//,在使用fromObject方法的时候会先进行参数类型的判断,这里就告诉我们,传入的参数是一个array类型
// //2、使用JSONArray
JSONArray listArray=JSONArray.fromObject(lists);
// list的对象 转化为数组对象
System.out.println("listArray:"+listArray.toString());
System.out.println( "======== 字符串转换为 对象=========");
//由于字符串的格式为带有“[]”的格式,所以这里选择JSONArray这个对象,它有toArray、toList方法可供使用,
// 前者转化为java中的数组,或者转化为java中的list,由于这里有实体类进行对应,所以在使用时指定了泛型的类型(Student.class),这样就可以得到转化后的对象。
String arr="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"一号\"},{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"一号\"}]";
//转化为list
List<Student> list2=(List<Student>)JSONArray.toList(JSONArray.fromObject(arr), Student.class);
for (Student student2 : list2) {
System.out.println(student2+"list");
}
//转化为数组
Student[] ss =(Student[])JSONArray.toArray(JSONArray.fromObject(arr),Student.class);
for (Student student2 : ss) {
System.out.println(student2+"数组");
}
System.out.println("===========map和json字符串的互转===============");
// JSON字符串不能直接转化为map对象,要想取得map中的键对应的值需要别的方式
String strObject="{\"first\":{\"address\":\"中国上海\",\"age\":\"23\",\"name\":\"JSON\"}}";
//使用toBean()方法是传入了三个参数,第一个是JSONObject对象,第二个是MyBean.class,第三个是一个Map对象。通过MyBean可以知道此类中要有一个first的属性,且其类型为Student,要和map中的键和值类型对应,即,first对应键 first类型对应值的类型。
//JSONObject
JSONObject jsonObject3=JSONObject.fromObject(strObject);
Map map=new HashMap();
map.put("first", Student.class);
//使用了toBean方法,需要三个参数
// 使用toBean()方法是传入了三个参数,第一个是JSONObject对象,第二个是MyBean.class,第三个是一个Map对象。通过MyBean可以知道此类中要有一个first的属性,且其类型为Student,要和map中的键和值类型对应,即,
// first对应键 first类型对应值的类型。
MyBean my=(MyBean)JSONObject.toBean(jsonObject3, MyBean.class, map);
System.out.println(my.getFirst());
// 主要说明了java中的bean、list、map和JSON的互转
}
}
MyBean实体类:
package com.jpa.demo.类型转换包;
import lombok.Data;
@Data
public class MyBean {
private Student first;
}
Student实体类:
package com.jpa.demo.类型转换包;
import lombok.Data;
@Data
public class Student {
private String name;
private String age;
private String address;
}
pom依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<!-- JSONObject.fromObject();-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>