package hb;
import java.util.Date;
public class Person {
String id;
int age;
String name;
Date birthday;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
package hb;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
public class JsonToList {
public static void main(String[] args) {
String json="[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]";
JSONArray jsonarray = JSONArray.fromObject(json);
System.out.println(jsonarray);
List list = (List)JSONArray.toCollection(jsonarray, Person.class);
Iterator it = list.iterator();
while(it.hasNext()){
Person p = (Person)it.next();
System.out.println(p.getAge());
}
}
@Test
public void jsonToList1(){
String json="[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]";
JSONArray jsonarray = JSONArray.fromObject(json);
System.out.println(jsonarray);
List list = (List)JSONArray.toList(jsonarray, Person.class);
Iterator it = list.iterator();
while(it.hasNext()){
Person p = (Person)it.next();
System.out.println(p.getAge());
}
}
@Test
public void jsonToList2(){
String json="[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]";
JSONArray jsonarray = JSONArray.fromObject(json);
System.out.println(jsonarray);
System.out.println("------------");
List list = (List)JSONArray.toList(jsonarray, new Person(), new JsonConfig());
Iterator it = list.iterator();
while(it.hasNext()){
Person p = (Person)it.next();
System.out.println(p.getAge());
}
}
}
|