//定义字符串map集合String jsondata = "https://www.520longzhigu.com/shenghuo/{"1":{"password":"123","username":"张三"},"2":{"password":"456","username":"李四"}}";//map集合字符串转为mapMap map2 = (Map)JSONObject.fromObject(jsondata);Set set = map2.keySet();//定义迭代器,迭代输出Iterator ite = set.iterator();while(ite.hasNext()) { //取出一个字符串对象 String key = (String)ite.next(); //转为json格式 JSONObject jsonObject = JSONObject.fromObject(map2.get(key)); //转为对象 Student stu = (Student)JSONObject.toBean(jsonObject, Student.class); System.out.println(key+""+stu);}
全部代码
import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set; import net.sf.json.JSONObject; public class Json { public static void main(String[] args) {//定义map集合Map map = new HashMap();map.put("1", new Student("张三", "123"));map.put("2", new Student("李四", "456"));//Map转json字符串JSONObject jsonMap = JSONObject.fromObject(map);System.out.println(jsonMap);//定义字符串map集合String jsondata = "https://www.520longzhigu.com/shenghuo/{"1":{"password":"123","username":"张三"},"2":{"password":"456","username":"李四"}}";//map集合字符串转为mapMap map2 = (Map)JSONObject.fromObject(jsondata);Set set = map2.keySet();//定义迭代器,迭代输出Iterator ite = set.iterator();while(ite.hasNext()) {//取出一个字符串对象String key = (String)ite.next();//转为json格式JSONObject jsonObject = JSONObject.fromObject(map2.get(key));//转为对象Student stu = (Student)JSONObject.toBean(jsonObject, Student.class);System.out.println(key+""+stu);} }}
运行结果:
SONArray与List互转定义list集合,List转型JSONArray
//定义list集合List<Student> list = new ArrayList<Student>();list.add(new Student("张三", "123"));list.add(new Student("李四", "456"));//List转型JSONArrayJSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray.toString());
JSONArray转型List,JSONArray是用的上面的那个jsonArray变量
//JSONArray转型ListList<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());Iterator<Student> ite =list2.iterator();while(ite.hasNext()) { Student stu = ite.next(); System.out.println(stu);}
全部代码
import java.util.ArrayList;import java.util.Iterator;import java.util.List; import net.sf.json.JSONArray;import net.sf.json.JsonConfig; public class Json { public static void main(String[] args) {//定义list集合List<Student> list = new ArrayList<Student>();list.add(new Student("张三", "123"));list.add(new Student("李四", "456"));//List转型JSONArrayJSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray.toString());//JSONArray转型ListList<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());Iterator<Student> ite =list2.iterator();while(ite.hasNext()) {Student stu = ite.next();System.out.println(stu);} }}
运行结果
JSONArray与数组互转定义数组,数组转JSONArray
//定义数组boolean[] boolArray = {true, false, true};//java数组转JSONArrayJSONArray jsonArray = JSONArray.fromObject(boolArray);System.out.println(jsonArray.toString());
JSONArray转java数组
//JSONArray转java数组Object obj[] = jsonArray.toArray();for(Object o : obj) { System.out.print(o+"t");}
全部代码
import net.sf.json.JSONArray; public class Json { public static void main(String[] args) {//定义数组boolean[] boolArray = {true, false, true};//java数组转JSONArrayJSONArray jsonArray = JSONArray.fromObject(boolArray);System.out.println(jsonArray.toString());//JSONArray转java数组Object obj[] = jsonArray.toArray();for(Object o : obj) {System.out.print(o+"t");} }}
以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!
「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助:- java电商项目模块介绍 java电商项目面试
- 华为手机红外摄像头介绍 被动红外探测器的工作原理
- 考研笔试分数线
- 三叉神经疼怎么引起的呢
- 曲师大美术考研真题
- 考研报名专业研究方向
- 梦见自己结婚办酒席的含义是好是坏?
- 如何解读做梦梦到怀孕的象征含义?
- 考研帮晒分
- 考研报名点是什么