Thursday, February 21, 2013

Gson convertion between Object & String


Following JAVA function can be used to convert "Response" object to "String".
Gson library need to add to project.

Gson gson=new Gson();
Response response=new Response();
response.setStatus("OK");
response.setMessage("Test JSON");
String rtnStr=gson.toJson(response);
return rtnStr;
   
This code snip convert "String" to "Response" object.
  
Gson gson=new Gson();
Response r=gson.fromJson(str, Response.class);