javabean:Address
package com.baebae.model; public class Address { private String city; private String district; private String street; private int no; getter/setter()... }
JAVA--JSON互相转换:
【1】JAVA——>JSON
/** * <1> JAVA-->JSON * 单个对象Object-->JSONObject * 集合List/Array/Set----->JSONArray */ @Test public void testObj2JSON() throws InterruptedException { // 1.bean---->json Address addr = new Address(); addr.setCity("北京"); addr.setDistrict("清河"); addr.setStreet("安宁庄东路"); addr.setNo(200); JSONObject jsonObj = JSONObject.fromObject(addr); System.err.println("____________jsonObj_______________"); System.out.println(jsonObj); // 1.集合-->JSONArray Set<Address> set = new HashSet<Address>(); Address[] addrArray = new Address[100]; List<Address> list = new ArrayList<Address>(); for(int i=0;i<10;i++){ Address address = new Address(); address.setCity("北京"); address.setDistrict("清河"); address.setStreet("安宁庄东路"+i+"号"); address.setNo(200+i); list.add(address); addrArray[i]=address; set.add(address); } // 这里不能用JSONObject.fromObject(collection) JSONArray jsonList = JSONArray.fromObject(list); JSONArray jsonArray = JSONArray.fromObject(addrArray); JSONArray jsonSet = JSONArray.fromObject(set); // 打印结果 Thread.sleep(3000); System.err.println("____________jsonList_______________"); System.out.println(jsonList); Thread.sleep(3000); System.err.println("_____________jsonArray______________"); System.out.println(jsonArray); Thread.sleep(3000); System.err.println("______________jsonSet_____________"); System.out.println(jsonSet); Thread.sleep(3000); }
打印结果:
____________jsonObj_______________ {"city":"北京","district":"清河","no":200,"street":"安宁庄东路"} ____________jsonList_______________ [{"city":"北京","district":"清河","no":200,"street":"安宁庄东路0号"},{"city":"北京","district":"清河","no":201,"street":"安宁庄东路1号"},{"city":"北京","district":"清河","no":202,"street":"安宁庄东路2号"},{"city":"北京","district":"清河","no":203,"street":"安宁庄东路3号"},{"city":"北京","district":"清河","no":204,"street":"安宁庄东路4号"},{"city":"北京","district":"清河","no":205,"street":"安宁庄东路5号"},{"city":"北京","district":"清河","no":206,"street":"安宁庄东路6号"},{"city":"北京","district":"清河","no":207,"street":"安宁庄东路7号"},{"city":"北京","district":"清河","no":208,"street":"安宁庄东路8号"},{"city":"北京","district":"清河","no":209,"street":"安宁庄东路9号"}] _____________jsonArray______________ [{"city":"北京","district":"清河","no":200,"street":"安宁庄东路0号"},{"city":"北京","district":"清河","no":201,"street":"安宁庄东路1号"},{"city":"北京","district":"清河","no":202,"street":"安宁庄东路2号"},{"city":"北京","district":"清河","no":203,"street":"安宁庄东路3号"},{"city":"北京","district":"清河","no":204,"street":"安宁庄东路4号"},{"city":"北京","district":"清河","no":205,"street":"安宁庄东路5号"},{"city":"北京","district":"清河","no":206,"street":"安宁庄东路6号"},{"city":"北京","district":"清河","no":207,"street":"安宁庄东路7号"},{"city":"北京","district":"清河","no":208,"street":"安宁庄东路8号"},{"city":"北京","district":"清河","no":209,"street":"安宁庄东路9号"},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null] ______________jsonSet_____________ [{"city":"北京","district":"清河","no":207,"street":"安宁庄东路7号"},{"city":"北京","district":"清河","no":202,"street":"安宁庄东路2号"},{"city":"北京","district":"清河","no":200,"street":"安宁庄东路0号"},{"city":"北京","district":"清河","no":204,"street":"安宁庄东路4号"},{"city":"北京","district":"清河","no":205,"street":"安宁庄东路5号"},{"city":"北京","district":"清河","no":208,"street":"安宁庄东路8号"},{"city":"北京","district":"清河","no":209,"street":"安宁庄东路9号"},{"city":"北京","district":"清河","no":203,"street":"安宁庄东路3号"},{"city":"北京","district":"清河","no":206,"street":"安宁庄东路6号"},{"city":"北京","district":"清河","no":201,"street":"安宁庄东路1号"}]
--------------------------------------------------------------------------------
【2】JSON---->JAVA
/** * <2> JSON-->JAVA * 1.如果字段名有错无法匹配,会发出警告;属性得到是null. * 2.转换: * ->Object * <1> 字符串封装成JSON 对象:【JSONObject.fromObject(jsonStr)】 * <2> JSON对象转换成Object:【(Address) JSONObject.toBean(jsonObject, Address.class)】 * ->Collection * <1> 字符串封装成JSONArray 对象:【JSONArray.fromObject(jsonArrayStr)】 * <2> JSONArray对象转换成Java集合:【(List<Address>)JSONArray.toCollection(jsonArray, Address.class)】 */ @Test public void testJson2Java() throws InterruptedException { // 1.JSON object String jsonStr = "{\"city\":\"北京\",\"district\":\"清河\",\"street\":\"安宁庄东路南口\",\"no\":500}"; // 3.JSONArray String jsonArrayStr = "[{\"city\":\"北京\",\"district\":\"清河\",\"street\":\"安宁庄东路南口\",\"no\":500},{\"city\":\"北京\",\"district\":\"上地\",\"street\":\"安宁庄东路\",\"no\":300}]"; JSONObject jsonObject = JSONObject.fromObject(jsonStr); JSONArray jsonArray = JSONArray.fromObject(jsonArrayStr); // JSONObject.toBean(jasonObject,Class): Bean; Address address = (Address) JSONObject.toBean(jsonObject, Address.class); List<Address> list = (List<Address>)JSONArray.toCollection(jsonArray, Address.class); // 1.输出Object Thread.sleep(1000); System.out.println("District:"+address.getDistrict()); // 2.输出Array Thread.sleep(1000); for(Address addr:list){ System.out.println(addr.getCity()+"\t"+addr.getDistrict()+"\t"+addr.getStreet()+"\t"+addr.getNo()); } }
打印结果:
District:清河 北京 清河 安宁庄东路南口 500 北京 上地 安宁庄东路 300
【总结】:
JSON————>JAVA:
->Object
<1> 字符串封装成JSON 对象:【JSONObject.fromObject(jsonStr)】
<2> JSON对象转换成Object:【(Address) JSONObject.toBean(jsonObject, Address.class)】
->Collection
<1> 字符串封装成JSONArray 对象:【JSONArray.fromObject(jsonArrayStr)】
<2> JSONArray对象转换成Java集合:【(List<Address>)JSONArray.toCollection(jsonArray, Address.class)】
JAVA————>JSON:
Object:【JSONObject.fromObject(addr)】
Collection:【JSONArray.fromObject(list)】
参考资料:
1.官方网站:http://sourceforge.net/projects/json-lib
2.所需jar包:
commons-beanutils-1.8.3.jar commons-collections-3.2.1.jar commons-lang-2.5.jar commons-logging-1.1.1.jar ezmorph-1.0.6.jar json-lib-2.4-jdk15.jar
相关推荐
- `json-lib-2.1-jdk15.jar`: JSON库,用于读写JSON格式的数据。 - `struts2-json-plugin-2.3.1.1.jar`: Struts2的JSON插件,用于处理JSON相关的请求和响应。 #### 2. 准备工作:搭建Struts2运行环境 - 创建一个...
首先,protobuf生成的对象通常包含了一些非标准的数据类型,如`byte[]`,这在直接使用常见的JSON库(如jsonlib)进行转换时可能会遇到问题。因为JSON规范要求键(key)必须是字符串,而protobuf对象的get方法可能...
2、vim /var/lib/docker/containers/container-ID/config.v2.json 修改配置文件中的目录位置,然后保存退出 “MountPoints”:{“/home”:{“Source”:”/docker”,”Destination”:”/home”,”RW”:true,”Name...
#### 九、小结 通过本章的学习,我们可以了解到AJAX的基本原理及其在JavaScript和jQuery中的应用。此外,还介绍了如何利用AJAX实现异步用户名校验的功能,并简要探讨了BaseServlet设计模式、JSON数据格式以及JSON-...
我们可以使用npm或yarn初始化项目,并配置tsconfig.json以支持TypeScript和Angular的特性。 首先,创建一个新的目录并初始化npm包: ``` mkdir my-ng-lib cd my-ng-lib yarn init ``` 接下来,安装必要的...
- **小结**:回顾并总结 Extjs 的基础知识,为后续的学习打下坚实的基础。 #### 四、消息框 - **基本概念**:了解消息框的作用和种类,包括提示框、输入框、确认框等。 - **实现方式**:学习如何在 Extjs 中实现...
#### 小结 通过学习本章节的内容,我们可以了解到 Rust 提供了一套完整的工具链来帮助开发者有效地管理大型项目,并通过智能指针和丰富的生态系统库支持高效的软件开发。理解这些概念和工具将大大提高 Rust 开发者...
### Extjs教程知识点总结 #### 第一章:序 - **主要内容**:简要介绍Extjs框架,并概述后续章节的学习内容。 #### 第二章:准备与资源 - **下载**:指导如何从官方...- **小结**:回顾综合项目的整体架构和技术要点。
这个名字是小羊驼的意思,因为这是一个小巧的lib,所以我认为它是一个完美的选择。 是的BDFL。 结帐事项:入门您可以使用快速安装您可以使用安装它。 pip install cira用法由于羊驼贸易API需要API密钥,因此您需要...