倚天杖
你可以用google-gson..详情:对象示例class BagOfPrimitives {
private int value1 = 1;
private String value2 = "abc";
private transient int value3 = 3;
BagOfPrimitives() {
// no-args constructor
}}(序列化)BagOfPrimitives obj = new BagOfPrimitives();Gson gson = new Gson();String json = gson.toJson(obj); ==> json is {"value1":1,"value2":"abc"}请注意,您不能使用循环引用序列化对象,因为这将导致无限递归。(反序列化)BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class); ==> obj2 is just like objGSON的另一个例子是:GSON易于学习和实现,您需要知道的是以下两种方法:->toJson()-将java对象转换为JSON格式->from Json()-将JSON转换为java对象import com.google.gson.Gson;public class TestObjectToJson {
private int data1 = 100;
private String data2 = "hello";
public static void main(String[] args) {
TestObjectToJson obj = new TestObjectToJson();
Gson gson = new Gson();
//convert java object to JSON format
String json = gson.toJson(obj);
System.out.println(json);
}}输出量{"data1":100,"data2":"hello"}资源:谷歌gson项目主页GSON用户指南例