介绍
这篇文章主要讲解了Java使用JSONObject操作json的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
在程序开发过程中,在参数传递,函数返回值等方面,越来越多的使用JSON.JSON (JavaScript对象表示法)是一种轻量级的数据交换格式,同时也易于机器解析和生成,易于理解,阅读和撰写,而且json采用完全独立于语言的文本格式,这使得json成为理想的数据交换语言只
json建构于两种结构:
"名称/值”对的集合(名称/值对的集合),在不同的语言中,它被理解为对象(对象),记录(记录),结构(结构),字典(字典),有趣列表(键列表),哈希表(哈希表)或者关联数组(关联数组)。
<强> JSONObject依赖:强>
最后一行需要保留,有两个jdk版本的实现:json -自由- 2.1 - jdk13.jar和json -自由- 2.1 jdk15。jar
& lt; dependency> & lt; groupId> net.sf.json-lib & lt; artifactId> json-lib & lt; version> 2.4 & lt;/version> & lt; classifier> jdk15 & lt;/dependency>
<强>使用net.sf。json需要导入的jar包强>
创建JSONObject,添加属性
//创建JSONObject JSONObject 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"));
判断输出对象的类型
布尔isArray=json.isArray (); 布尔isEmpty=json.isEmpty (); 布尔isNullObject=json.isNullObject (); System.out.println(“是否数组:“+ isArray +“,是否空:“+ isEmpty +“,是否空为空对象:“+ isNullObject);
把JSONArray添加到JSONObject中
/JSONArray把添加到JSONObject中 JSONArray JSONArray JSONArray=new (); 获取。add(0,“张三“); 获取。添加(“123”);//开始添加 jsonArray json.element (“student"); System.out.println (json);
全部代码:
进口net.sf.json.JSONArray; 进口net.sf.json.JSONObject; 公开课Json { 公共静态void main (String [] args) {//创建JSONObject JSONObject 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"));//判断输出对象的类型 布尔isArray=json.isArray (); 布尔isEmpty=json.isEmpty (); 布尔isNullObject=json.isNullObject (); System.out.println(“是否数组:“+ isArray +“,是否空:“+ isEmpty +“,是否空为空对象:“+ isNullObject); System.out.println (“=====?;//把JSONArray添加到JSONObject中 JSONArray JSONArray JSONArray=new (); 获取。add(0,“张三“); 获取。添加(“123”);//开始添加 jsonArray json.element (“student"); System.out.println (json); } }
运行结果:
创JSONArray建,添加属性值
//创JSONArray建 JSONArray JSONArray JSONArray=new ();//添加 获取。add(0,“张三“); 获取。添加(“123”); jsonArray.element(“男“); 系统。
根据下标返回输出
System.out.println (jsonArray.get (0));
根据下标设置新值,修改
获取。集(0,“李四“); System.out.println(获取);
把JSONObject放入到jsonArray中
//把JSONObject放入到获取中 JSONObject JSONObject=new JSONObject (); jsonObject.put (“username",“张三“); jsonObject.put (“password",“123“); jsonArray.add (jsonObject); 系统。
全部代码:
进口net.sf.json.JSONArray; 进口net.sf.json.JSONObject; 公开课Json { 公共静态void main (String [] args) {//创JSONArray建 JSONArray JSONArray JSONArray=new ();//添加 获取。add(0,“张三“); 获取。添加(“123”); jsonArray.element(“男“); System.out.println(获取);//根据下标返回输出 System.out.println (jsonArray.get (0));//根据下标设置新值,修改 获取。集(0,“李四“); System.out.println(获取);//把JSONObject放入到获取中 JSONObject JSONObject=new JSONObject (); jsonObject.put (“username",“张三“); jsonObject.put (“password",“123“); jsonArray.add (jsonObject); System.out.println(获取);//循环输出 for (int i=0;我& lt;jsonArray.size ();我+ +){ System.out.println (jsonArray.get (i)); } } }Java使用JSONObject操作json的方法