日期:2014-05-16 浏览次数:20338 次
可以直接从Json中将队形解析出来,这个Json必须是一个Json对象,如下:
{"name":"zhangsan","age":22,"address":"太原"}
它只是一个对象。
我新建一个类,生成get和set方法,如下:
package com.zhycheng; public class Human { String name,address; int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
String s="{\"name\":\"zhangsan\",\"age\":22,\"address\":\"太原\"}"; Gson g=new Gson(); Human h=g.fromJson(s, Human.class); System.out.println(h.getName()+";"+h.getAge()+";"+h.getAddress());
[{"name":"zhangsan","age":22,"address":"太原"},
{"name":"lisi","age":20,"address":"黄冈"},
{"name":"wangwu","age":21,"address":"武汉"}]
代码如下:String s="[{\"name\":\"zhangsan\",\"age\":22,\"address\":\"太原\"},{\"name\":\"lisi\",\"age\":20,\"address\":\"黄冈\"},{\"name\":\"wangwu\",\"age\":21,\"address\":\"武汉\"}]"; Type type=new TypeToken<LinkedList<Human>>(){}.getType(); Gson g=new Gson(); LinkedList<Human> ll=g.fromJson(s, type); for(int i=0;i<ll.size();i++) { Human h=(Human)ll.get(i); System.out.println(h.getName()+","+h.getAge()+","+h.getAddress()); }