List转换为JSONArray二
 package com.beijxing.TestMain;

 import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List; import com.beijxing.entity.Student; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; /**
* List<Object>转换为JSONArray
* @author ywp
* @version 2016年10月23日21:36:08
*/
public class TestJson1 {
public static void main(String[] args) {
createJSONArray();
}
public static void createJSONArray(){
Student student = new Student();
student.setId(1);
student.setName("杨文培");
student.setSex("男");
//student.setAge(23);
student.setBirthday("1993-01-23");
List<String> list = new ArrayList<String>();
list.add("篮球");
list.add("乒乓球");
list.add("羽毛球");
student.setHobby(list); Student student2 = new Student();
student2.setId(1);
student2.setName("杨文培");
student2.setSex("男");
student.setAge(23);
student2.setBirthday("1993-01-23");
List<String> list2 = new ArrayList<String>();
list2.add("篮球");
list2.add("乒乓球");
list2.add("羽毛球");
student2.setHobby(list2); List<Student> list3 = new ArrayList<Student>();
list3.add(student);
list3.add(student2);
JSONArray jsonArray = JSONArray.fromObject(list3);
System.out.println("jsonArray="+jsonArray); JSONArray new_jsonArray = JSONArray.fromObject(jsonArray.toArray());
//迭代器迭代
Collection<JSONObject> collection = JSONArray.toCollection(new_jsonArray);
if(collection!=null&&!collection.isEmpty()){
Iterator<JSONObject> iterator = collection.iterator();
while(iterator.hasNext()){
JSONObject jsonObject = JSONObject.fromObject(iterator.next());
Student student4 = (Student) jsonObject.toBean(jsonObject,Student.class);
System.out.println(student4);
}
}
//foreach方式迭代
/* for (Object object : new_jsonArray) {
JSONObject jsonObject = (JSONObject) object;
Student student3 = (Student) jsonObject.toBean(jsonObject,Student.class);
System.out.println(student3);
}*/
}
}
上一篇:MySQL篇,第四章:数据库知识4


下一篇:相声段子:How Are You