文末会有读者福利

简介:

在程序开发过程中,在参数传递,函数返回值等方面,越来越多的使用JSON。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,同时也易于机器解析和生成、易于理解、阅读和撰写,而且Json采用完全独立于语言的文本格式,这使得Json成为理想的数据交换语言。

JSON建构于两种结构:

“名称/值”对的集合(A Collection of name/value pairs),在不同的语言中,它被理解为对象(Object), 记录(record), 结构(struct), 字典(dictionary), 有趣列表(keyed list), 哈希表(hash table)或者关联数组(associative array)。

JSONObject依赖:

最后一行需要保留,有两个jdk版本的实现:json-lib-2.1-jdk13.jar和json-lib-2.1-jdk15.jar

net.sf.json-lib      json-lib      2.4jdk15

使用net.sf.json需要导入的jar包

jar包下载:https://pan.baidu.com/s/1iZiXw55TPwIxYFQQCaR9Gw

JSONObject

创建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);

根据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);

把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

创建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字符串互转

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}@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());}}

输出结果:

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 = "[{"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);}}

运行结果

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

//定义字符串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互转

定义list集合,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转型List,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+"");}

全部代码

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+"");}}}

运行结果:

感谢阅读 喜欢的朋友和爱学习的小伙伴可关注下笔者 会定期发布优质文章的哦!

以下是笔者整理的大量的java面试和结构资料,有需要的朋友需‘关注’+‘私信’【资料】即可获得

本文链接:https://blog.csdn.net/qq_40205116/article/details/102921564

java jsonobject 转对象_解析JSON中JSONObject的高级使用相关推荐

  1. jsonobject转map对象_解析JSON中JSONObject的高级使用

    简介: 在程序开发过程中,在参数传递,函数返回值等方面,越来越多的使用JSON.JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,同时也易于机器解析和生成.易 ...

  2. 【PI应用】用Java查询雅虎天气并解析Json数据

    这篇文章是"树莓派查询天气,通过串口彩屏/7219点阵实时显示"的一部分,主要介绍使用Java查询雅虎天气并解析Json返回数据,这里只是将获得的实时天气.未来十天的天气等信息,解 ...

  3. java.lang.IllegalArgumentException: 字符[_]在域名中永远无效。 at

    [http-nio-8080-exec-1] org.apache.coyote.AbstractProcessor.parseHost [xxx_tomcat] 是无效主机注意:更多的请求解析错误将 ...

  4. ajax解析json中的对象数组对象,在JQuery中检索json数组后获取json对象Ajax

    我使用JQuery AJAX检索某些数据(标题和说明).正如你可以看到我打通的结果,并出结果的JSON数组和循环div标签中在JQuery中检索json数组后获取json对象Ajax success ...

  5. android json字符串转成json对象_在PHP中处理JSON数组以及对象

    php中文网最新课程 每日17点准时技术干货分享 在 PHP 中处理 JSON 数组以及对象 与客户端混淆的常见原因是围绕 JSON 数组和对象,以及如何在 PHP 中指定他们.特别是,问题是由空对象 ...

  6. java 阿拉伯语_解析Json阿拉伯语文本

    我无法从SQL数据库中解析阿拉伯语/波斯语文本 . 一切都设置为UTF-8 . 我的SQL数据库文本设置为 utf8_general_ci . JSON解析器也设置为UTF-8 . 文字显示英文很好 ...

  7. java处理json特殊字符_如何处理JSON中的特殊字符

    JSON 是适用于 Ajax 应用程序的一种有效格式,原因是它使 JavaScript 对象和字符串值之间得以快速转换.由于 Ajax 应用程序非常适合将纯文本发送给服务器端程序并对应地接收纯文本,相 ...

  8. eval解析JSON中的注意点

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 使用eval函数来解析,并且使用jquery的ea ...

  9. jquery eval解析JSON中的注意点介绍

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 使用eval函数来解析,并且使用jquery的ea ...

最新文章

  1. 领域驱动设计_软件核心复杂性应对之道
  2. 如何监听WebView完成加载URL?
  3. Java 帝国之消息队列
  4. linux下开发python爬虫_linux入门教程—开发常用命令
  5. uniapp 判断页面是否是横竖屏,解决微信小程序video组件全屏播放视频遮盖自定义播放控件问题
  6. 使用V-chart时配置踩过的一些坑
  7. linux 嵌入式串口通信设计目的,基于linux的嵌入式串口通信.doc
  8. Netty源码学习(零)前言
  9. vb.net变量值变化触发事件_Angular变化检测的理解
  10. 2018最火机器学习项目盘点—CV项目领冠榜单
  11. java restful项目打包_66-JT项目04(项目打包发布/JSON/项目业务)
  12. Win下Eclipse提交hadoop程序出错:org.apache.hadoop.security.AccessControlException: Permission denied: user=
  13. Gradle下载的地址
  14. 基于maven自动构建和部署工具-JDeploy
  15. haproxy frontend 和backend
  16. Mounty 1.10免费版(NTFS硬盘工具)支持big sur
  17. [高数][高昆轮][高等数学上][第一章-函数与极限]03.函数的极限
  18. 嵌入式硬件学习之嵌入式软件和硬件的区别
  19. Vrep/CoppeliaSim:安装及相关资料
  20. 华为2018勇敢星实习招聘机试题

热门文章

  1. [渝粤教育] 广东-国家-开放大学 10763k2_客户服务管理_21秋考试
  2. 面试题目_总结面试中 promise 相关题目的套路
  3. 下载anaconda时出现“Please make sure you are connected to the internet”警告
  4. js结合css3,使用JS和CSS3实现的旗帜飘扬动画
  5. flask对mysql数据库增删改查_Flask学习(三) - Flask-SQLAlchemy对数据库增删改查
  6. 【OFDM系列2】OFDM复信号符号信噪比EsN0、比特信噪比EbN0、SNR的含义及关系详解
  7. android数据流分类,【Android工程之类】1 MVVM架构 - MVVM与单向数据流
  8. java描边_shape描边设置是否显示四周描边
  9. 如何有效开展小组教学_高效课堂 有效教学 | 教育部专家到徐州市第三中学开展教研活动...
  10. NOIP模拟测试21「折纸·不等式」