说明:数据类型:hashMap、ArrayList、String、Object…
JSONObject moJsonObj = JSON.parseObject(JSON.toJSONString(mo));
JSONArray jsonArray1 = JSON.parseArray(JSON.toJSONString(l1));
//json对象转为实体类对象
Person person4 = JSON.parseObject(person1.toString(), Person.class);
//json字符串类型的实体类集合转化为对应的实体类集合
List<Person> people = JSON.parseArray(jsonArray1.toString(), Person.class);
//json字符串类型的集合的集合类型转化为对应集合的集合
List<List> personList = JSON.parseArray(jsonArray2.toString(), List.class);
//json字符串类型的map集合转化为对应map的集合
List<Map> maps = JSON.parseArray(jsonArray3.toString(), Map.class);
//jison字符串类型的HashMap转化为对应的map对象
Map map = JSON.parseObject(moJsonObj.toJSONString(), HashMap.class);
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
package com.cfay.pojo;
public class Person {
private String username;//用户名
private Integer age;//年龄
private Double weight;//体重
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
public Person() {
}
public Person(String username, Integer age, Double weight) {
this.username = username;
this.age = age;
this.weight = weight;
}
@Override
public String toString() {
return "Person{" +
"username='" + username + '\'' +
", age=" + age +
", weight=" + weight +
'}';
}
}
package com.cfay;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cfay.pojo.Person;
import java.lang.reflect.Field;
import java.util.*;
public class TestJson {
/**
* 对JSONObject、JSONArray、实体类之间的简单转换操作
*/
public static void main(String[] args) {
testHashMap();
testJSONArray();
dataTransformation();
}
/**
*
* 1、JSON.toJSONString(hp)==>转为json字符串类型
* 2、JSON.parseObject(json字符串)==>转为json对象
*/
private static void testHashMap() {
//定义
HashMap<String, Object> hp = new HashMap<>();
//赋值
Person p1 = new Person("lcb", 18, 120.0d);
Person p2 = new Person("laj", 18, 100.0d);
Person p3 = new Person("楚风岸影", 24, 118.0d);
hp.put("person1", p1);
hp.put("person2", p2);
hp.put("person3", p3);
//操作
//把HashMap转为JSONObject
//1.1
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(hp));
//1.2(只有hashmap可以)
JSONObject jsonObject1 = new JSONObject(hp);
//获取JSONObject中的指定具体对象
JSONObject person1 = jsonObject.getJSONObject("person1");
JSONObject person2 = (JSONObject) jsonObject.get("person2");
//将json对象转换为实体类对象
Person person3 = JSON.toJavaObject(person1, Person.class);
Person person4 = JSON.parseObject(person1.toString(), Person.class);
//获取指定对象属性的值
Double weight = person1.getDouble("weight");
//Double weight1 = (Double) person1.get("weight");
String username = person1.getString("username");
Integer age = person1.getInteger("age");
//System.out.println(username + "--" + age + "--" + weight);
//key值获取:jsonObject.keySet();
//value值获取:jsonObject.values();
//遍历value
//jsonObject.values().forEach(l-> System.out.println(l));
//2.1、利用反射,把json对象转换为实体类对象
try {
Person o = (Person) ChangeJsonObjToObj(jsonObject.getJSONObject("person1"), Person.class);
System.out.println(o);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 数据类型: 集合、Map、对象...
* 数据类型和json的互转
* 1、JSON.toJSONString(数据类型)==>将对应的数据类型转化为json字符串
* 2、JSON.parseObject(json类型的字符串, 数据类型.class);
* 3、JSON.parseArray(json类型的字符串, 数据类型.class);
*/
private static void dataTransformation() {
//数据类型定义
List l1 = new ArrayList();
List l2 = new ArrayList();
List l3 = new ArrayList();
Map<String, Object> mo = new HashMap();
//数据类型初始化
for (int i = 0; i < 4; i++) {
Person person = new Person("lcb" + i, 18 + i, 18.9d);
l1.add(person);
mo.put("person" + i, person);
}
l2.add(l1);
l3.add(mo);
//数据类型转json
JSONArray jsonArray1 = JSON.parseArray(JSON.toJSONString(l1));
JSONArray jsonArray2 = JSON.parseArray(JSON.toJSONString(l2));
JSONArray jsonArray3 = JSON.parseArray(JSON.toJSONString(l3));
JSONObject moJsonObj = JSON.parseObject(JSON.toJSONString(mo));
//json转数据类型
//①数据类型 obj = JSON.parseObject(字符串类型的json数据类型, 数据类型.class);
//②List<数据类型> obj = JSON.parseArray(字符串类型的json数据类型, 数据类型.class);
//转化为对应的实体类集合
List<Person> people = JSON.parseArray(jsonArray1.toString(), Person.class);
//转化为对应集合的集合
List<List> personList = JSON.parseArray(jsonArray2.toString(), List.class);
//转化为对应map的集合
List<Map> maps = JSON.parseArray(jsonArray3.toString(), Map.class);
//转化为对应的map对象
Map map = JSON.parseObject(moJsonObj.toJSONString(), HashMap.class);
}
/**
* JSONArray的简单使用
*/
private static void testJSONArray() {
HashMap<String, Object> hashMap1 = new HashMap<>();
Person p1 = new Person("lcb", 18, 120.0d);
hashMap1.put("person1", p1);
Person p2 = new Person("laj", 18, 100.0d);
hashMap1.put("person2", p2);
ArrayList<HashMap<String, Object>> list = new ArrayList<>();
list.add(hashMap1);
//把ArrayList集合转为jsonArray
//JSON.toJSONString(list)==>把集合转为json字符串
//JSON.parseArray(s)==>把json字符串转为jsonArray
JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(list));
//JSONArray jsonArray = JSONObject.parseArray(JSON.toJSONString(list));
//获取jsonArray中的JSONObject
JSONObject job = jsonArray.getJSONObject(0);
//把JSONObject转为实体类对象
Person person = JSONObject.toJavaObject(job.getJSONObject("person1"), Person.class);
Person person2 = JSONObject.toJavaObject((JSONObject) job.get("person2"), Person.class);
Person person3 = JSON.parseObject(job.get("person2").toString(), Person.class);
//System.out.println(person + " <=l=> " + person2 + " <=l=> "+person3);
List<Map> listList = JSON.parseArray(jsonArray.toJSONString(), Map.class);
}
/**
* 把json对象转换位pojo实体类
*
* @param jsonObject
* @param pojo
* @return 转换后的实体类对象
* @throws Exception
*/
public static Object ChangeJsonObjToObj(JSONObject jsonObject, Class pojo) throws Exception {
//获取pojo的字段
Field[] fields = pojo.getDeclaredFields();
//根据传入的Class动态生成pojo对象
Object obj = pojo.newInstance();
for (Field field : fields) {
//设置字段可访问
field.setAccessible(true);
//获取字段的属性名
String name = field.getName();
//如果字段在JSONObject对象中不存在,就跳过
try {
jsonObject.get(name);
} catch (Exception e) {
continue;
}
if (jsonObject.get(name) != null && !"".equals(jsonObject.getString(name))) {
//把字段转化为相应的类型并设置到生成的对象中
if (field.getType().equals(Long.class) || field.getType().equals(long.class)) {
field.set(obj, Long.parseLong(jsonObject.getString(name)));
} else if (field.getType().equals(String.class)) {
field.set(obj, jsonObject.getString(name));
} else if (field.getType().equals(Double.class) || field.getType().equals(double.class)) {
field.set(obj, jsonObject.getDouble(name));
} else if (field.getType().equals(Integer.class) || field.getType().equals(int.class)) {
field.set(obj, jsonObject.getInteger(name));
} else if (field.getType().equals(Date.class)) {
field.set(obj, jsonObject.getDate(name));
} else {
continue;
}
}
}
return obj;
}
}