标签:rgs style get div from rom str color getname
包:gson-2.3.jar
Json文本解析为Java对象;Java对象生成为Json文本
import com.google.gson.Gson; public class TestGson { class Person { private String name; private int[] power; private Horse hs1; private Horse[] hs2; public Horse getHs1() { return hs1; } public void setHs1(Horse hs1) { this.hs1 = hs1; } public Horse[] getHs2() { return hs2; } public void setHs2(Horse[] hs2) { this.hs2 = hs2; } public int[] getPower() { return power; } public void setPower(int[] power) { this.power = power; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class Horse { private String type; public String getType() { return type; } public void setType(String type) { this.type = type; } } public static void main(String[] args) { String strJson = "{‘name‘:‘吕奉先‘,‘power‘:[100,30],‘hs1‘:{‘type‘:‘赤兔‘},‘hs2‘:[{‘type‘:‘乌骓‘},{‘type‘:‘黄标‘}]}"; Gson g = new Gson(); // Json->对象 Person p = g.fromJson(strJson, Person.class); System.out.println(p.name + ":" + p.power[1]); //对象->Json p.setName("关羽"); String json = g.toJson(p); System.out.println(json); } }
标签:rgs style get div from rom str color getname
原文地址:http://www.cnblogs.com/AndyHoo/p/6099493.html