一天一小步,人生一大步。
├学会总结╀学会记录╀学会生活┦

GSON jar包:http://code.google.com/p/google-gson/

//java object to json
public String toJson(Object obj){
    Gson gson = new Gson();
    return gson.toJson(obj);
}
//json to java object
public Object toObject(String jsonStr, Class c){
    Gson gson = new Gson();
    return gson.fromJson(jsonStr, c);

}

 

jackson jar 包: http://jackson.codehaus.org/

//java object to json
public String toJson(Object obj){
    ObjectMapper om = new ObjectMapper ();
    return om.writeValueAsString(obj);
}
//json to java object
public Object toObject(String json, Class c){
    ObjectMapper mapper = new ObjectMapper();
    User user = mapper.readValue(json, c);
}

 

 

posted on 2013-03-01 10:04  .Xm  阅读(659)  评论(0编辑  收藏  举报