Source From Here
Preface
In this tutorial, we will show you how to use Gson to convert Java object to / from JSON. Below is the basic description of Gson:
Note.
1. Quick Reference
1.1 toJson() – Convert Java object to JSON
Output:
1.2 fromJson() – Convert JSON to Java object
2. FAQs
Some commonly ask questions.
2.1 Convert a JSON Array to a List, using TypeToken
Output:
2.2 Convert a JSON to a Map
Output:
2.3 Enable JSON pretty print feature in Gson
1. By default, Gson display the JSON output like the following :
Output:
2. To enable the pretty-print, create the Gson object with GsonBuilder
Output:
Preface
In this tutorial, we will show you how to use Gson to convert Java object to / from JSON. Below is the basic description of Gson:
Note.
1. Quick Reference
1.1 toJson() – Convert Java object to JSON
- package demo.gson;
- import java.io.FileWriter;
- import java.util.ArrayList;
- import java.util.List;
- import com.google.gson.Gson;
- public class People {
- public String name = null;
- public int age = 0;
- public List
addr = new ArrayList(); - public boolean isSingle=false;
- @Override
- public String toString()
- {
- StringBuffer strBuf = new StringBuffer(String.format("Name: %s\n", name));
- strBuf.append(String.format("Age: %d\n", age));
- strBuf.append(String.format("IsSingle? %s\n", isSingle));
- strBuf.append("Addr:\n");
- for(String a:addr) strBuf.append(String.format("\t%s\n", a));
- return strBuf.toString();
- }
- public static void ToJson() throws Exception
- {
- People p = new People();
- p.name = "John"; p.age = 36; p.addr.add("abc"); p.addr.add("123"); p.isSingle=true;
- Gson gson = new Gson();
- // 1. Java object to JSON, and save into a file
- FileWriter fw = new FileWriter("john.json");
- gson.toJson(p, fw);
- fw.flush(); fw.close();
- // 2. Java object to JSON, and assign to a String
- String jsonInString = gson.toJson(p);
- System.out.printf("%s\n", jsonInString);
- }
- }
1.2 fromJson() – Convert JSON to Java object
- public static void FromJson() throws Exception{
- Gson gson = new Gson();
- // 1. JSON to Java object, read it from a file.
- FileReader fw = new FileReader("john.json");
- People p1 = gson.fromJson(fw, People.class);
- System.out.printf("P1:\n%s\n", p1);
- fw.close();
- // 2. JSON to Java object, read it from a Json String.
- String jsonInString = "{'name':'John','age':36,'addr':['abc','123'],'isSingle':true}";
- People p2 = gson.fromJson(jsonInString, People.class);
- System.out.printf("P2:\n%s\n", p2);
- // JSON to JsonElement, convert to String later.
- JsonElement json = gson.fromJson(new FileReader("john.json"), JsonElement.class);
- String result = gson.toJson(json);
- System.out.printf("P3:\n%s\n", result);
- }
Some commonly ask questions.
2.1 Convert a JSON Array to a List, using TypeToken
- package demo.gson;
- import java.util.List;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- public class Movie {
- public String name = "";
- public int cost = 0;
- public Movie(String n, int c){this.name = n; this.cost = c;}
- @Override
- public String toString()
- {
- return String.format("%s/%d", name, cost);
- }
- public static void main(String args[])
- {
- String json = "[{\"name\":\"Scary Movie\",\"cost\":50}, {\"name\":\"Action Movie\",\"cost\":50}]";
- Gson gson = new Gson();
- List
list = gson.fromJson(json, new TypeToken - >(){}.getType());
- for(Movie m:list) {System.out.printf("%s\n", m);}
- }
- }
2.2 Convert a JSON to a Map
- public static void CovJson2Map()
- {
- String json = "{\"name\":\"Comedy Movie\",\"cost\":30}";
- Gson gson = new Gson();
- Map
map = gson.fromJson(json, new TypeToken - map.forEach((x,y)->System.out.printf("%s|%s\n", x, y));
- }
2.3 Enable JSON pretty print feature in Gson
1. By default, Gson display the JSON output like the following :
- Gson gson = new Gson();
- People p = new People();
- p.name = "John"; p.age = 36; p.addr.add("abc"); p.addr.add("123"); p.isSingle=true;
- String json = gson.toJson(p);
- System.out.println(json);
2. To enable the pretty-print, create the Gson object with GsonBuilder
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
- People p = new People();
- p.name = "John"; p.age = 36; p.addr.add("abc"); p.addr.add("123"); p.isSingle=true;
- String json = gson.toJson(p);
- System.out.println(json);
- {
- "name": "John",
- "age": 36,
- "addr": [
- "abc",
- "123"
- ],
- "isSingle": true
- }
java编程基本样本
回覆刪除以位为单位打印无符号整数
My pleasure. Glad you like it.
回覆刪除