本文由 发布,转载请注明出处,如有问题请联系我们! 发布时间: 2021-08-01jsonobject判断key是否存在某个key-java的foreach用法
加载中在软件开发全过程中,JSON在参数传递,函数返回值等领域的运用愈来愈多。JSON(JavaScript Object notification)是一种轻量的数据传输文件格式,也极易被设备分析和转化成,了解,载入和载入。Json选用非常单独于语言表达的文档格式,这促使JSON变成一种理想化的数据传输语言表达。
JSON以二种构造搭建:
名字/值对的结合被了解为不一样编程语言中的目标,纪录,构造,词典,键控目录,哈希表或关系二维数组。
JSONObject相互依赖:
最终一行必须保存,有两个jdk版本:json-lib-2.1-jdk13.jar和json-lib-2.1-jdk15.jar..
net.sf.json-lib json-lib 2.4 jdk15导进应用net.sf.json需要的jar包
Jar包免费下载:https://pan.baidu.com/s/1iZiXw55TPwIxYFQQCaR9Gw.
目标
建立JSONObject并增加特性。
//建立JSONObjectJSONObject json = new JSONObject();//加上特性json.put("username", "张三");json.put("password", "123");//打印出System.out.println(json); //提升特性json.element("sex", "男");json.put("age", 18);System.out.println(json);依据密匙。
System.out.println(json.get("sex"));分辨輸出目标的种类。
boolean isArray = json.isArray();boolean isEmpty = json.isEmpty();boolean isNullObject = json.isNullObject();System.out.println("是不是二维数组:" isArray ", 是不是空:" isEmpty ", 是不是空为空目标:" isNullObject);将JSONArray加上到JSONObject。
/把JSONArray加上到JSONObject中JSONArray jsonArray = new JSONArray();jsonArray.add(0, "张三");jsonArray.add(1, "123");//逐渐加上json.element("student", jsonArray);System.out.println(json);全部编码:
import net.sf.json.JSONArray;import net.sf.json.JSONObject; public class Json {public static void main(String[] args) {//建立JSONObjectJSONObject json = new JSONObject();//加上特性json.put("username", "张三");json.put("password", "123");//打印出System.out.println(json);//提升特性json.element("sex", "男");json.put("age", 18);System.out.println(json);//依据key回到System.out.println(json.get("sex"));//分辨輸出目标的种类boolean isArray = json.isArray();boolean isEmpty = json.isEmpty();boolean isNullObject = json.isNullObject();System.out.println("是不是二维数组:" isArray ", 是不是空:" isEmpty ", 是不是空为空目标:" isNullObject);System.out.println("=====");//把JSONArray加上到JSONObject中JSONArray jsonArray = new JSONArray();jsonArray.add(0, "张三");jsonArray.add(1, "123");//逐渐加上json.element("student", jsonArray);System.out.println(json);}}运作結果:
目标建立JSONArray并增加特性值。
//建立JSONArrayJSONArray jsonArray = new JSONArray();//加上jsonArray.add(0, "张三");jsonArray.add(1, "123");jsonArray.element("男");System.依据字符回到輸出。
System.out.println(jsonArray.get(0));依据字符设定新值,改动。
jsonArray.set(0, "李四");System.out.println(jsonArray);把JSONObject放进到JSONArray中//把JSONObject放进到JSONArray中JSONObject jsonObject = new JSONObject();jsonObject.put("username", "张三");jsonObject.put("password", "123");jsonArray.add(jsonObject);System.全部编码:
import net.sf.json.JSONArray;import net.sf.json.JSONObject; public class Json {public static void main(String[] args) {//建立JSONArrayJSONArray jsonArray = new JSONArray();//加上jsonArray.add(0, "张三");jsonArray.add(1, "123");jsonArray.element("男");System.out.println(jsonArray);//依据字符回到輸出System.out.println(jsonArray.get(0));//依据字符设定新值,改动jsonArray.set(0, "李四");System.out.println(jsonArray);//把JSONObject放进到JSONArray中JSONObject jsonObject = new JSONObject();jsonObject.put("username", "张三");jsonObject.put("password", "123");jsonArray.add(jsonObject);System.out.println(jsonArray);//循环系统輸出for(int i = 0; i < jsonArray.size(); i ) {System.out.println(jsonArray.get(i));}}}运作結果
JavaBean和json字符串数组互相变换。学员班集体:
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}@Overridepublic 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 = "{"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 = "{"username":"李四", "password":"123"}";//变为json目标JSONObject json = JSONObject.fromObject(jsondata);//变为JavaBean目标Student stu2 = (Student)JSONObject.toBean(json, Student.class);System.out.println(stu2.toString());}}輸出結果:
将listjson字符串数组译成。最先界定目录结合,并将目录变换为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字符串数组变换为目录。
//json字符串数组转listList list2 = new ArrayList();String jsondata = "[{"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 = "[{"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);}}运作結果
变换为地形图json字符串数组和从地形图JSON字符串数组变换。界定地形图结合,并将地形图变换为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集合String jsondata = "{"1":{"password":"123","username":"张三"},"2":{"password":"456","username":"李四"}}";//map集合字符串数组变为mapMap map2 = (Map)JSONObject.fromObject(jsondata);Set set = map2.keySet();//界定迭代器,迭代更新輸出Iterator ite = set.iterator();while(ite.hasNext()) {//取下一个字符串数组目标String key = (String)ite.next();//变为json文件格式JSONObject jsonObject = JSONObject.fromObject(map2.get(key));//变为目标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转json字符串数组JSONObject jsonMap = JSONObject.fromObject(map);System.out.println(jsonMap);//界定字符串数组map集合String jsondata = "{"1":{"password":"123","username":"张三"},"2":{"password":"456","username":"李四"}}";//map集合字符串数组变为mapMap map2 = (Map)JSONObject.fromObject(jsondata);Set set = map2.keySet();//界定迭代器,迭代更新輸出Iterator ite = set.iterator();while(ite.hasNext()) {//取下一个字符串数组目标String key = (String)ite.next();//变为json文件格式JSONObject jsonObject = JSONObject.fromObject(map2.get(key));//变为目标Student stu = (Student)JSONObject.toBean(jsonObject, Student.class);System.out.println(key " " stu);}}}运作結果:
SONArray和List互相迁移。界定目录结合,并将目录变换为JSONArray。
//界定list结合List list = new ArrayList();list.add(new Student("张三", "123"));list.add(new Student("李四", "456"));//List转型发展JSONArrayJSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray.toString());JSONArray变换目录,JSONArray是上边应用的JsonArray自变量。
//JSONArray转型发展ListList list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());Iterator 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 list = new ArrayList();list.add(new Student("张三", "123"));list.add(new Student("李四", "456"));//List转型发展JSONArrayJSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray.toString());//JSONArray转型发展ListList list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());Iterator ite = list2.iterator();while(ite.hasNext()) {Student stu = ite.next();System.out.println(stu);}}}运作結果
JSONArray和二维数组互换。界定一个二维数组,并将该二维数组变换为JSONArray。
//定义数组boolean[] boolArray = {true, false, true};//java二维数组转JSONArrayJSONArray jsonArray = JSONArray.fromObject(boolArray);System.out.println(jsonArray.toString());JSONArray到java二维数组。
//JSONArray转java二维数组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) {//定义数组boolean[] boolArray = {true, false, true};//java二维数组转JSONArrayJSONArray jsonArray = JSONArray.fromObject(boolArray);System.out.println(jsonArray.toString());//JSONArray转java二维数组Object obj[] = jsonArray.toArray();for(Object o : obj) {System.out.print(o "t");}}}