//定義字符串map集合String jsondata = "https://www.520longzhigu.com/shenghuo/{"1":{"password":"123","username":"張三"},"2":{"password":"456","username":"李四"}}";//map集合字符串轉(zhuǎn)為mapMap map2 = (Map)JSONObject.fromObject(jsondata);Set set = map2.keySet();//定義迭代器,迭代輸出Iterator ite = set.iterator();while(ite.hasNext()) { //取出一個(gè)字符串對(duì)象 String key = (String)ite.next(); //轉(zhuǎn)為json格式 JSONObject jsonObject = JSONObject.fromObject(map2.get(key)); //轉(zhuǎn)為對(duì)象 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轉(zhuǎn)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集合字符串轉(zhuǎn)為mapMap map2 = (Map)JSONObject.fromObject(jsondata);Set set = map2.keySet();//定義迭代器,迭代輸出Iterator ite = set.iterator();while(ite.hasNext()) {//取出一個(gè)字符串對(duì)象String key = (String)ite.next();//轉(zhuǎn)為json格式JSONObject jsonObject = JSONObject.fromObject(map2.get(key));//轉(zhuǎn)為對(duì)象Student stu = (Student)JSONObject.toBean(jsonObject, Student.class);System.out.println(key+""+stu);} }}運(yùn)行結(jié)果:
SONArray與List互轉(zhuǎn)定義list集合,List轉(zhuǎn)型JSONArray
//定義list集合List<Student> list = new ArrayList<Student>();list.add(new Student("張三", "123"));list.add(new Student("李四", "456"));//List轉(zhuǎn)型JSONArrayJSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray.toString());JSONArray轉(zhuǎn)型List,JSONArray是用的上面的那個(gè)jsonArray變量
//JSONArray轉(zhuǎn)型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轉(zhuǎn)型JSONArrayJSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray.toString());//JSONArray轉(zhuǎn)型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);} }}運(yùn)行結(jié)果
JSONArray與數(shù)組互轉(zhuǎn)定義數(shù)組,數(shù)組轉(zhuǎn)JSONArray
//定義數(shù)組boolean[] boolArray = {true, false, true};//java數(shù)組轉(zhuǎn)JSONArrayJSONArray jsonArray = JSONArray.fromObject(boolArray);System.out.println(jsonArray.toString());JSONArray轉(zhuǎn)java數(shù)組
//JSONArray轉(zhuǎn)java數(shù)組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) {//定義數(shù)組boolean[] boolArray = {true, false, true};//java數(shù)組轉(zhuǎn)JSONArrayJSONArray jsonArray = JSONArray.fromObject(boolArray);System.out.println(jsonArray.toString());//JSONArray轉(zhuǎn)java數(shù)組Object obj[] = jsonArray.toArray();for(Object o : obj) {System.out.print(o+"t");} }}
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問(wèn)題,請(qǐng)您及時(shí)就醫(yī)或請(qǐng)專(zhuān)業(yè)人士給予相關(guān)指導(dǎo)!
「愛(ài)刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對(duì)您有所幫助:- java電商項(xiàng)目模塊介紹 java電商項(xiàng)目面試
- 華為手機(jī)紅外攝像頭介紹 被動(dòng)紅外探測(cè)器的工作原理
- 考研筆試分?jǐn)?shù)線
- 三叉神經(jīng)疼怎么引起的呢
- 曲師大美術(shù)考研真題
- 考研報(bào)名專(zhuān)業(yè)研究方向
- 夢(mèng)見(jiàn)自己結(jié)婚辦酒席的含義是好是壞?
- 如何解讀做夢(mèng)夢(mèng)到懷孕的象征含義?
- 考研幫曬分
- 考研報(bào)名點(diǎn)是什么
