運(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ì)您有所幫助:- java電商項(xiàng)目模塊介紹 java電商項(xiàng)目面試
- 華為手機(jī)紅外攝像頭介紹 被動(dòng)紅外探測(cè)器的工作原理
- 考研筆試分?jǐn)?shù)線
- 三叉神經(jīng)疼怎么引起的呢
- 曲師大美術(shù)考研真題
- 考研報(bào)名專業(yè)研究方向
- 夢(mèng)見自己結(jié)婚辦酒席的含義是好是壞?
- 如何解讀做夢(mèng)夢(mèng)到懷孕的象征含義?
- 考研幫曬分
- 考研報(bào)名點(diǎn)是什么
