convert Java object to / from JSON
2013-07-12 15:26
387 查看
JSON is stand for JavaScript Object Notation, it is a lightweight data-interchange format. You can see many Java applications started to throw away XML format and start using json as a new s data-interchange format. Java is all about object, often times,
you need to convert an object into json format for data-interchange or vice verse.
In this article, we show you how to use
Gson, JSON library, to convert object to/from json.
Gson is easy to learn and implement, what we need to know are following two methods
toJson() – Convert Java object to JSON format
fromJson() – Convert JSON into Java object
Gson official site, for Maven user, declares following dependency in your pom.xml.
Output
Output
you need to convert an object into json format for data-interchange or vice verse.
In this article, we show you how to use
Gson, JSON library, to convert object to/from json.
Gson is easy to learn and implement, what we need to know are following two methods
toJson() – Convert Java object to JSON format
fromJson() – Convert JSON into Java object
1. Gson Dependency
For non-Maven user, get the Gson library fromGson official site, for Maven user, declares following dependency in your pom.xml.
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>1.7.1</version> </dependency>
2. POJO
A pojo, with initialized values. Later use Gson to convert this object to/from JSON formatted string.package com.mkyong.core; import java.util.ArrayList; import java.util.List; public class DataObject { private int data1 = 100; private String data2 = "hello"; private List<String> list = new ArrayList<String>() { { add("String 1"); add("String 2"); add("String 3"); } }; //getter and setter methods @Override public String toString() { return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list=" + list + "]"; } }
3. toJson() example
Convert object to JSON string, and save it as “file.json“.package com.mkyong.core; import java.io.FileWriter; import java.io.IOException; import com.google.gson.Gson; public class GsonExample { public static void main(String[] args) { DataObject obj = new DataObject(); Gson gson = new Gson(); // convert java object to JSON format, // and returned as JSON formatted string String json = gson.toJson(obj); try { //write converted json data to a file named "file.json" FileWriter writer = new FileWriter("c:\\file.json"); writer.write(json); writer.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println(json); } }
Output
{"data1":100,"data2":"hello","list":["String 1","String 2","String 3"]}
4. fromJson() example
Read data from “file.json“, convert back to object and display it.package com.mkyong.core; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import com.google.gson.Gson; public class GsonExample { public static void main(String[] args) { Gson gson = new Gson(); try { BufferedReader br = new BufferedReader( new FileReader("c:\\file.json")); //convert the json string back to object DataObject obj = gson.fromJson(br, DataObject.class); System.out.println(obj); } catch (IOException e) { e.printStackTrace(); } } }
Output
DataObject [data1=100, data2=hello, list=[String 1, String 2, String 3]]
相关文章推荐
- How to convert Java object to / from JSON (Gson)
- Java JSON - Convert Java Object To / From JSON (Jackson)
- How to convert Java object to / from JSON (Gson)
- java.lang.String cannot be convert to JSONObject
- convert NameValueCollection/Dictionary<string, object> to JSON string
- JSONObject.fromObject(map)(JSON与JAVA数据的转换)
- 在eclipse使用到json时出现The type java.lang.Object cannot be resolved. It is indirectly referenced from req
- java java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 5 to TIMESTAMP.
- of type java.lang.String cannot be converted to JSONObject: Android
- google-gson - A Java library to convert JSON to Java objects and vice-versa
- Failed to convert from type java.lang.String to type java.util.Date for value………解决办法
- Introduction to Java Script Object Notation (JSON)
- how to convert a object to json file immediately during debug?
- Type mismatch: cannot convert from Object to Member
- 关于JSON to JAVA object的java.io.EOFException异常
- 《Springboot极简教程》MappingMongoConverter:Failed to convert from type [java.lang.String] to type [long] for value 'null'; ne
- Convert string to JSONObject by net.sf.json library
- IOS json转对象 /json 转 Object / ios json to Object / ios json convert Object
- Object convert to Json string
- 40 lines of java code Map convert to JSON