听说点赞关注的人,身体健康,万事如意,工作顺利,爱情甜蜜,一夜暴富,升职加薪……最终迎娶白富美!!!
‼️微信公众号:炜煜工作室
🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱
接口组成示例
JSON格式数据
JSON格式的数据举例
JSON格式的数据本质是字符串,其主要是key:value,也就是键和值的方式组合的。有点儿类似字典数据类型的数据。{}里面的表示一个对象,[]里面表示一个数组,key:value可以理解为对象中的属性和值。
{"name":"Jhon","age":14,"sex":0}
JSON的简单使用
Person实体类
public class Person implements Serializable {
private String name;
private int age;
private boolean sex;
private double weight;
public Person() {}
public Person(String name, int age, boolean sex, double weight) {
this.name = name;
this.age = age;
this.sex = sex;
this.weight = weight;}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && sex == person.sex && Double.compare(person.weight, weight) == 0 && Objects.equals(name, person.name);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", weight=" + weight +
'}'; }
@Override
public int hashCode() {
return Objects.hash(name, age, sex, weight); }
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 boolean isSex() {
return sex;}
public void setSex(boolean sex) {
this.sex = sex;}
public double getWeight() {
return weight;}
public void setWeight(double weight) {
this.weight = weight;}
}
封装的JSONUtils类;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import java.lang.reflect.Type;
import java.util.ArrayList;
public class JsonUtil { //单例引用
public static JsonUtil instance;
private static Gson gson;
public static JsonUtil getInstance() {
if (instance == null) {
synchronized (JsonUtil.class) {
if (instance == null) {
instance = new JsonUtil();
synchronized (Gson.class) {
gson = new Gson();
}
}
}
}
return instance;
}
public <T> String beanToJsonStr(T t) { //将对象转换为JSON的字符串
String str = "";
if (t != null) {
str = gson.toJson(t);
}
return str;
}
public <T> T json2Bean(String jsonStr, Class<T> tClass) { //将JSON格式的字符串转换为对象
T t = null;
if (jsonStr != null) {
try {
t = gson.fromJson(jsonStr, tClass);
} catch (JsonIOException e) {
}
}
return t;
}
public static <T> ArrayList<T> json2ArrayList(String json, Class<T> clazz) {
//将JSON的字符串转换为对象数组
Type type = new TypeToken<ArrayList<JsonObject>>() {
}.getType();
ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json, type);
ArrayList<T> list = new ArrayList<>();
for (JsonObject jsonObject:
jsonObjects) {
list.add(new Gson().fromJson(jsonObject, clazz));
}
return list;
}
}
使用
public class JSONTest {
Person person = new Person("jack",18,false,52.3);
String jsonStr = JsonUtil.getInstance().beanToJsonStr(person);
Person person2 = JsonUtil.getInstance().json2Bean(jsonStr,Person.class);
}
🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱
表情网站:[🎁 Emoji cheat sheet for GitHub, Basecamp, Slack & more (webfx.com)](