import com.google.gson.Gson ; import java.util.List ; import java.util.Map ; import java.util.HashMap ; import java.util.Arrays ; public class TestGson { public static class Foo { public int a ; public String b ; public int[] c ; public List文档见此。d ; public Map e ; } public static void main (String[] args) { Foo bar = new Foo (); bar.a = 1 ; bar.b = "abc" ; bar.c = new int[] {6,7,8} ; bar.d = Arrays.asList ("hello", "world") ; bar.e = new HashMap () ; bar.e.put ("zoo", 3) ; bar.e.put ("keeper", 5) ; Gson gson = new Gson () ; String s = gson.toJson (bar) ; System.out.println (s) ; Foo bb = gson.fromJson (s, Foo.class) ; System.out.println (gson.toJson (bb)) ; } }
Thursday, April 11, 2013
Gson
JSON 的 serialization 库,挺好用。
Subscribe to:
Posts (Atom)