一. Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。
Gson里最重要的对象有2个Gson 和 GsonBuilder。
Gson有2个最基本的方法
1) toJson() – 转换java 对象到JSON
2) fromJson() – 转换JSON到java对象
二. 几个简单的例子。
exp1(一个简单对象object和json之间的转换):
public class MsgInfo2 { private String message; private String flag; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } } public class Test1 { public static void main(String[] args) { MsgInfo2 m2 = new MsgInfo2(); m2.setFlag("00"); m2.setMessage("message"); tojson(m2); String json = "{'message':'message1','flag':'00'}"; toObject(json); } public static void tojson(MsgInfo2 m) { // 将对象编译成json Gson gson = new Gson(); String result = gson.toJson(m); System.out.println(result); // 输出:{"message":"message","flag":"00"} } public static void toObject(String m) { // 将json编译成对象 Gson gson = new Gson(); MsgInfo2 msg = gson.fromJson(m, MsgInfo2.class); System.out.println(msg.getFlag());//输出:00 System.out.println(msg.getMessage());//输出:message1 } }
exp2(一个复杂对象object和json之间的转换):
json的结构如下:
{
"org": {
"orgId": "orgId",
"orgName": "orgName"
},
"biz": [
{
"appcode": 55,
"subscode": "subscode0"
},
{
"appcode": 66,
"subscode": "subscode1"
}
]
}
public class ComOpen { private Org org; private List<BizObject> biz; public Org getOrg() { return org; } public void setOrg(Org org) { this.org = org; } public List<BizObject> getBiz() { return biz; } public void setBiz(List<BizObject> biz) { this.biz = biz; } } public class Org { private String orgId; private String orgName; public String getOrgId() { return orgId; } public void setOrgId(String orgId) { this.orgId = orgId; } public String getOrgName() { return orgName; } public void setOrgName(String orgName) { this.orgName = orgName; } } public class BizObject { private int appcode; private String subscode; public String getSubscode() { return subscode; } public void setSubscode(String subscode) { this.subscode = subscode; } public int getAppcode() { return appcode; } public void setAppcode(int appcode) { this.appcode = appcode; } } /** * 测试java对象和json转换 * * @author yangliang * @version 1.0 * @created 2011-10-11 下午03:14:04 * @history * @see */ public class Test2 { public static void main(String[] args) { toJson(); toObject(); } public static void toJson(){ ComOpen como = new ComOpen(); Org org = new Org(); org.setOrgId("orgId"); org.setOrgName("orgName"); como.setOrg(org); List<BizObject> biz = new ArrayList<BizObject>(); BizObject bizObject = new BizObject(); bizObject.setAppcode(55); bizObject.setSubscode("subscode0"); BizObject bizObject1 = new BizObject(); bizObject1.setAppcode(66); bizObject1.setSubscode("subscode1"); biz.add(bizObject); biz.add(bizObject1); como.setBiz(biz); Gson gson=new GsonBuilder().create(); String result = gson.toJson(como); System.out.println(result); //输出{"org":{"orgId":"orgId","orgName":"orgName"},"biz":接下行 //[{"appcode":55,"subscode":"subscode0"},{"appcode":66,"subscode":"subscode1"}]} } public static void toObject(){ String json = "{'org':{'orgId':'orgId','orgName':'orgName'},'biz':" + "[{'appcode':55,'subscode':'subscode0'},{'appcode':66,'subscode':'subscode1'}]}"; Gson gson=new Gson(); ComOpen m3 = gson.fromJson(json, ComOpen.class); System.out.println(m3.getBiz().size());//2 System.out.println("orgId:"+m3.getOrg().getOrgId());//orgId:orgId System.out.println("appcode:"+ m3.getBiz().get(0).getAppcode());//appcode:55 } }
三.其他可参考gson教程和官方帮助文档