Android 使用GSON / JAVA使用genson

来源:互联网 发布:手游编程用什么语言 编辑:程序博客网 时间:2024/05/20 15:12

gson

1.下载gson-2.3.1.jar , 放到项目libs里

2.对象转json字符串

Gson gson=new Gson();String senddatastr=gson.toJson(obj);
3.json字符串转对象

Gson gson=new Gson();return gson.fromJson(packetContent.toString(),PackageCls.class);
可以对返回结果再强制转换一次

JAVA使用genson

genson

Genson genson = newGenson();
String json = genson.serialize(777.777); // the output will be 777.777
genson.serialize(true); // output is true (without quotes)
 
genson.deserialize("777"int.class); // deserializes it into 777
genson.deserialize("777.777", Object.class); // will return 777.777 (a double)
genson.deserialize("null", Object.class); // will return null;

0 1