list与jSONArray互相转换
以下为list与jSONArray互相转换代码。 javaBean代码: package com.test.json;
转换测试代码: package com.test.json; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONArray; public class TestJSONObjectList { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { Student student1 = new Student(); student1.setId(1); student1.setName("jag"); student1.setSex("man"); student1.setAge(25); student1.setHobby(new String[]{"篮球","上网","跑步","游戏"}); Student student2 = new Student(); student2.setId(1); student2.setName("jag"); student2.setSex("man"); student2.setAge(25); student2.setHobby(new String[]{"篮球","游戏"}); List<Student> list = new ArrayList<Student>(); list.add(student1); list.add(student2); // List 转 JSONArray JSONArray jsonMessage = JSONArray.fromObject(list); //输出转换后的JSON串 System.out.println(jsonMessage.toString()); List<Student> listO = (List<Student>) JSONArray.toCollection(JSONArray.fromObject(jsonMessage),Student.class); Student student = null; for(int i = 0; i < listO.size(); i++) { student = listO.get(i); System.out.println(student.getName()); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |