List<Object>转换为JSONArray二
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了List<Object>转换为JSONArray二,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2190字,纯文字阅读大概需要4分钟。
内容图文
![List<Object>转换为JSONArray二](/upload/InfoBanner/zyjiaocheng/1329/e06443cd096640958f3ae47bcbf14c44.jpg)
1 package com.beijxing.TestMain; 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.Iterator; 6 import java.util.List; 7 8 import com.beijxing.entity.Student; 9 10 import net.sf.json.JSONArray; 11 import net.sf.json.JSONObject; 12 13 /** 14 * List<Object>转换为JSONArray 15 * @author ywp 16 * @version 2016年10月23日21:36:08 17 */ 18 public class TestJson1 { 19 public static void main(String[] args) { 20 createJSONArray(); 21 } 22 public static void createJSONArray(){ 23 Student student = new Student(); 24 student.setId(1); 25 student.setName("杨文培"); 26 student.setSex("男"); 27//student.setAge(23);28 student.setBirthday("1993-01-23"); 29 List<String> list = new ArrayList<String>(); 30 list.add("篮球"); 31 list.add("乒乓球"); 32 list.add("羽毛球"); 33 student.setHobby(list); 3435 Student student2 = new Student(); 36 student2.setId(1); 37 student2.setName("杨文培"); 38 student2.setSex("男"); 39 student.setAge(23); 40 student2.setBirthday("1993-01-23"); 41 List<String> list2 = new ArrayList<String>(); 42 list2.add("篮球"); 43 list2.add("乒乓球"); 44 list2.add("羽毛球"); 45 student2.setHobby(list2); 4647 List<Student> list3 = new ArrayList<Student>(); 48 list3.add(student); 49 list3.add(student2); 50 JSONArray jsonArray = JSONArray.fromObject(list3); 51 System.out.println("jsonArray="+jsonArray); 5253 JSONArray new_jsonArray = JSONArray.fromObject(jsonArray.toArray()); 54//迭代器迭代55 Collection<JSONObject> collection = JSONArray.toCollection(new_jsonArray); 56if(collection!=null&&!collection.isEmpty()){ 57 Iterator<JSONObject> iterator = collection.iterator(); 58while(iterator.hasNext()){ 59 JSONObject jsonObject = JSONObject.fromObject(iterator.next()); 60 Student student4 = (Student) jsonObject.toBean(jsonObject,Student.class); 61 System.out.println(student4); 62 } 63 } 64//foreach方式迭代65/* for (Object object : new_jsonArray) { 66 JSONObject jsonObject = (JSONObject) object; 67 Student student3 = (Student) jsonObject.toBean(jsonObject,Student.class); 68 System.out.println(student3); 69 }*/70 } 71 }
转换为JSONArray二' ref='nofollow'>List
原文:http://www.cnblogs.com/yangly/p/5991432.html
内容总结
以上是互联网集市为您收集整理的List<Object>转换为JSONArray二全部内容,希望文章能够帮你解决List<Object>转换为JSONArray二所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。