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

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

運(yùn)行結(jié)果
JavaBean與json字符串互轉(zhuǎn)student類:
public class Student { private String username; private String password; public String getUsername() {return username; } public void setUsername(String username) {this.username = username; } public String getPassword() {return password; } public void setPassword(String password) {this.password = password; } public Student(String username, String password) {super();this.username = username;this.password = password; } public Student() {super();// TODO Auto-generated constructor stub } @Override public String toString() {return "Student [username=" + username + ", password=" + password + "]"; }}定義對(duì)象,JavaBean對(duì)象轉(zhuǎn)json字符串
//定義對(duì)象Student stu = new Student("張三", "123456");//JavaBean對(duì)象轉(zhuǎn)json字符串JSONObject jsonObject = JSONObject.fromObject(stu);System.out.println(jsonObject);json字符串轉(zhuǎn)為javaBean
//json字符串轉(zhuǎn)為javaBean//定義json字符串String jsondata = "https://www.520longzhigu.com/shenghuo/{"username":"李四", "password":"123"}";//轉(zhuǎn)為json對(duì)象JSONObject json = JSONObject.fromObject(jsondata);//轉(zhuǎn)為JavaBean對(duì)象Student stu2 = (Student)JSONObject.toBean(json, Student.class);System.out.println(stu2.toString());全部代碼:
import net.sf.json.JSONObject; public class Json { public static void main(String[] args) {//定義對(duì)象Student stu = new Student("張三", "123456");//JavaBean對(duì)象轉(zhuǎn)json字符串JSONObject jsonObject = JSONObject.fromObject(stu);System.out.println(jsonObject);//json字符串轉(zhuǎn)為javaBean//定義json字符串String jsondata = "https://www.520longzhigu.com/shenghuo/{"username":"李四", "password":"123"}";//轉(zhuǎn)為json對(duì)象JSONObject json = JSONObject.fromObject(jsondata);//轉(zhuǎn)為JavaBean對(duì)象Student stu2 = (Student)JSONObject.toBean(json, Student.class);System.out.println(stu2.toString()); }}輸出結(jié)果:
List與json字符串互轉(zhuǎn)先定義list集合,list轉(zhuǎn)json字符串
//定義list集合List list = new ArrayList();list.add(new Student("張三", "123"));list.add(new Student("李四", "456"));//list轉(zhuǎn)json字符串JSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray);json字符串轉(zhuǎn)list
//json字符串轉(zhuǎn)listList list2 = new ArrayList();String jsondata = "https://www.520longzhigu.com/shenghuo/[{"password":"123","username":"張三"},{"password":"456","username":"李四"}]";JSONArray jsonArray1 = JSONArray.fromObject(jsondata);for(int i = 0; i < jsonArray1.size(); i++) { JSONObject jsonObject2 = jsonArray1.getJSONObject(i); Student stu2 = (Student)JSONObject.toBean(jsonObject2, Student.class); list2.add(stu2);}System.out.println(list2);全部代碼
import java.util.ArrayList;import java.util.List; import net.sf.json.JSONArray;import net.sf.json.JSONObject; public class Json { public static void main(String[] args) {//定義list集合List list = new ArrayList();list.add(new Student("張三", "123"));list.add(new Student("李四", "456"));//list轉(zhuǎn)json字符串JSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray);//json字符串轉(zhuǎn)listList list2 = new ArrayList();String jsondata = "https://www.520longzhigu.com/shenghuo/[{"password":"123","username":"張三"},{"password":"456","username":"李四"}]";JSONArray jsonArray1 = JSONArray.fromObject(jsondata);for(int i = 0; i < jsonArray1.size(); i++) {JSONObject jsonObject2 = jsonArray1.getJSONObject(i);Student stu2 = (Student)JSONObject.toBean(jsonObject2, Student.class);list2.add(stu2);}System.out.println(list2); }}運(yùn)行結(jié)果
Map與json字符串互轉(zhuǎn)定義map集合,Map轉(zhuǎn)json字符串
//定義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集合,map集合字符串轉(zhuǎn)為map


以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請(qǐng)您及時(shí)就醫(yī)或請(qǐng)專業(yè)人士給予相關(guān)指導(dǎo)!

「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對(duì)您有所幫助: