Download Gson jar from this link
Quick Reference
toJson()
– Convert Java object to JSON
Gson gson = new Gson();
<Java class> obj = new
<Java class>
();
gson.toJson(obj, new FileWriter("C:\\file.json"));
String jsonInString = gson.toJson(obj);
fromJson()
– Convert JSON to Java object
Gson gson = new Gson();
<Java class>
staff = gson.fromJson(new FileReader("D:\\file.json"),
<Java class>
.class);
String jsonInString = "{'name' : 'mkyong'}";
Staff staff = gson.fromJson(jsonInString,
<Java class>
.class);
JsonElement json = gson.fromJson(new FileReader("D:\\file.json"), JsonElement.class);
String result = gson.toJson(json);
No comments:
Post a Comment