亚洲精品久久久久久第一页-人妻少妇精彩视品一区二区三区-91国产自拍免费视频-免费一级a在线播放视频正片-少妇天天日天天射天天爽-国产大屁股喷水视频在线观看-操美女骚穴抽插性爱视频-亚洲 欧美 中文字幕 丝袜-成人免费无码片在线观看

java的foreach用法 jsonobject判斷key是否存在某個(gè)key( 三 )


//定義字符串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ì)您有所幫助: