1 package com.beijxing.TestMain; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.beijxing.entity.Student; 7 8 import net.sf.json.JSONArray; 9 10 /** 11 * List<Object>转换为JSONArray 12 * @author ywp 13 * @version 2016年10月23日21:36:08 14 */ 15 public class TestJson1 { 16 public static void main(String[] args) { 17 createJSONArray(); 18 } 19 public static void createJSONArray(){ 20 Student student = new Student(); 21 student.setId(1); 22 student.setName("杨文培"); 23 student.setSex("男"); 24 //student.setAge(23); 25 student.setBirthday("1993-01-23"); 26 List<String> list = new ArrayList<String>(); 27 list.add("篮球"); 28 list.add("乒乓球"); 29 list.add("羽毛球"); 30 student.setHobby(list); 31 32 Student student2 = new Student(); 33 student2.setId(1); 34 student2.setName("杨文培"); 35 student2.setSex("男"); 36 //student.setAge(23); 37 student2.setBirthday("1993-01-23"); 38 List<String> list2 = new ArrayList<String>(); 39 list2.add("篮球"); 40 list2.add("乒乓球"); 41 list2.add("羽毛球"); 42 student2.setHobby(list2); 43 44 List<Student> list3 = new ArrayList<Student>(); 45 list3.add(student); 46 list3.add(student2); 47 JSONArray jsonArray = JSONArray.fromObject(list3); 48 System.out.println("jsonArray="+jsonArray); 49 } 50 }
时间: 2024-12-15 15:35:14