java的foreach用法 jsonobject判断key是否存在某个key( 二 )

运行结果
JavaBean与json字符串互转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 + "]"; }}定义对象,JavaBean对象转json字符串
//定义对象Student stu = new Student("张三", "123456");//JavaBean对象转json字符串JSONObject jsonObject = JSONObject.fromObject(stu);System.out.println(jsonObject);json字符串转为javaBean
//json字符串转为javaBean//定义json字符串String jsondata = "https://www.520longzhigu.com/shenghuo/{"username":"李四", "password":"123"}";//转为json对象JSONObject json = JSONObject.fromObject(jsondata);//转为JavaBean对象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) {//定义对象Student stu = new Student("张三", "123456");//JavaBean对象转json字符串JSONObject jsonObject = JSONObject.fromObject(stu);System.out.println(jsonObject);//json字符串转为javaBean//定义json字符串String jsondata = "https://www.520longzhigu.com/shenghuo/{"username":"李四", "password":"123"}";//转为json对象JSONObject json = JSONObject.fromObject(jsondata);//转为JavaBean对象Student stu2 = (Student)JSONObject.toBean(json, Student.class);System.out.println(stu2.toString()); }}输出结果:
List与json字符串互转先定义list集合,list转json字符串
//定义list集合List list = new ArrayList();list.add(new Student("张三", "123"));list.add(new Student("李四", "456"));//list转json字符串JSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray);json字符串转list
//json字符串转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转json字符串JSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray);//json字符串转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); }}运行结果
Map与json字符串互转定义map集合,Map转json字符串
//定义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集合,map集合字符串转为map


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: