import org.json.jsonobject;
public class orgjsondemo {
public static void main(string[] args) {
//创建测试object
user user = new user("李宁",24,"北京");
system.out.println(user);
//转成json字符串
string json = new jsonobject(user).tostring();
system.out.println(json);
//json字符串转成对象
jsonobject jsonobject = new jsonobject(json);
string name = jsonobject.getstring("name");
integer age = jsonobject.getint("age");
string location = jsonobject.getstring("location");
user user1 = new user(name,age,location);
system.out.println(user1);
}
}
import com.google.gson.gson;
public class gsondemo {
public static void main(string[] args) {
//创建测试object
user user = new user("李宁",24,"北京");
system.out.println(user);
//转成json字符串
gson gson = new gson();
string json = gson.tojson(user);
system.out.println(json);
//json字符串转成对象
user user1 = gson.fromjson(json,user.class);
system.out.println(user1);
}
}
import com.alibaba.fastjson.json;
public class fastjsondemo {
public static void main(string[] args) {
//创建测试object
user user = new user("李宁",24,"北京");
system.out.println(user);
//转成json字符串
string json = json.tojson(user).tostring();
system.out.println(json);
//json字符串转成对象
user user1 = json.parseobject(json,user.class);
system.out.println(user1);
}
}