首页 > 代码库 > json数组和List转换

json数组和List转换

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());        }            }}
package hb;import java.util.LinkedList;import java.util.List;import net.sf.json.JSONArray;public class ListToJson {    public static void main(String[] args) {        List list = new LinkedList();        for(int i=0;i<3;i++){            Person p = new Person();            p.setAge(i);            p.setName("name"+i);            list.add(p);        }        JSONArray jsonarray = JSONArray.fromObject(list);        System.out.println(jsonarray);    }}
[{"age":0,"birthday":null,"id":"","name":"name0"},{"age":1,"birthday":null,"id":"","name":"name1"},{"age":2,"birthday":null,"id":"","name":"name2"}]

 

使用的是json-lib.jar包

将json格式的字符数组转为List对象

 

 

转载自:http://hbiao68.iteye.com/blog/1771155

 

 

另外自己项目中使用hibernate 查询出来的是List<HashMap>,

转换过程 List<HashMap>----->JSONArray---->List<业务对象>  如果是联合查询,则这个业务对象需要加入新的成员变量,

如果加入了并不希望有数据库字段映射,就在属性上加上 @Transient

public class PaymentReq implements Serializable {    private static final long serialVersionUID = -1515222020971048819L;//     u.userId,u.userName,u.openId,u.sex,u.phone,u.userPaymentMoney,pr.paymentReqId,pr.addTime    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long paymentReqId;    private String addTime;    private Long userId;        @Transient    private String userName;    @Transient    private String openId;    @Transient    private String sex;    @Transient    private String phone;    @Transient    private String userPaymentMoney;
public List<HashMap> findAllPaymentReqByUserId(Long userId){            Session session = sessionFactory.getCurrentSession();            StringBuffer sql=new StringBuffer("select u.userId,u.userName,u.openId,u.sex,u.phone,u.userPaymentMoney,pr.paymentReqId,pr.addTime ");            sql.append("from user u,paymentreq pr ");            sql.append("where u.userId=pr.userId ");            sql.append("and u.userPaymentMoney>‘0‘ ");            sql.append("order by pr.addTime asc");            Query query = session.createSQLQuery(sql.toString()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);            List<HashMap> list = query.list();            return list;        }
@Test    public void testfindAllPaymentReqByUserId(){        List<HashMap> list=paymentReqService.findAllPaymentReqByUserId(1L);        JSONArray jsonArray=JSONArray.fromObject(list);         List list_ = (List)JSONArray.toCollection(jsonArray, PaymentReq.class);           for(int i=0;i<list_.size();i++){             PaymentReq pr=(PaymentReq)list_.get(i);             System.out.println(pr);         }            }
//打印结果PaymentReq [paymentReqId=1, addTime=2014-11-15 19:27:23, userId=1, userName=text, openId=1, sex=0, phone=18681883016, userPaymentMoney=2.01]PaymentReq [paymentReqId=2, addTime=2014-11-15 19:27:23, userId=2, userName=text, openId=null, sex=0, phone=18681883015, userPaymentMoney=10]

json数组和List转换