JSONObject、JSONArray方法使用详解
来源:互联网 发布:软件部职责要求 编辑:程序博客网 时间:2024/05/22 08:23
JSON在线API网址:http://tool.oschina.net/apidocs/apidoc?api=json-lib2.4
1.JSONObject介绍
JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包。
2.下载jar包
http://files.cnblogs.com/java-pan/lib.rar
提供了除JSONObject的jar之外依赖的其他6个jar包,一共7个jar文件
说明:因为工作中项目用到的版本是1.1的对应jdk1.3的版本,故本篇博客是基于1.1版本介绍的。
对应此版本的javadoc下载路径如下:http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-1.1/
目前最新的版本为2.4,其他版本下载地址为http://sourceforge.net/projects/json-lib/files/json-lib/
JSONObject类
是一个final类,继承了Object,实现了JSON接口
构造方法如下:
JSONObject();创建一个空的JSONObject对象
JSONObject(boolean isNull);创建一个是否为空的JSONObject对象
普通方法如下:
fromBean(Object bean);静态方法,通过一个pojo对象创建一个JSONObject对象
fromJSONObject(JSONObject object);静态方法,通过另外一个JSONObject对象构造一个JSONObject对象
fromJSONString(JSONString string);静态方法,通过一个JSONString创建一个JSONObject对象
toString();把JSONObject对象转换为json格式的字符串
iterator();返回一个Iterator对象来遍历元素
接下来就是一些put/get方法,需要普通的get方法和pot方法做一下强调说明,API中是这样描述的:
A get method returns a value if one can be found, and throws an exception if one cannot be found. An opt method returns a default value instead of throwing an exception, and so is useful for obtaining optional values.
JSONArray类
是一个final类,继承了Object,实现了JSON接口
构造方法如下:
JSONArray();构造一个空的JSONArray对象
普通方法如下:
fromArray(Object[] array);静态方法,通过一个java数组创建一个JSONArray对象
fromCollection(Collection collection);静态方法,通过collection集合对象创建一个JSONArray对象
fromString(String string);静态方法,通过一个json格式的字符串构造一个JSONArray对象
toString();把JSONArray对象转换为json格式的字符串
iterator();返回一个Iterator对象来遍历元素
接下来同样是put/get方法……
XMLSerializer:Utility class for transforming JSON to XML an back.
一个继承自Object的类
构造方法如下:
XMLSerializer();创建一个XMLSerializer对象
普通方法如下:
setRootName(String rootName);设置转换的xml的根元素名称
setTypeHintsEnabled(boolean typeHintsEnabled);设置每个元素是否显示type属性
write(JSON json);把json对象转换为xml,默认的字符编码是UTF-8,
需要设置编码可以用write(JSON json, String encoding)
原文:http://blog.csdn.net/sunny_na/article/details/54630341
- JSONObject、JSONArray方法使用详解
- JSONObject、JSONArray方法使用详解
- json,jsonObject , jsonArray 详解
- JSONObject和JSONArray详解
- JSONObject、JSONArray 格式详解
- JSONObject JSONArray 对象使用
- 使用JSONObject和JSONArray
- JSONObject和JSONArray使用
- JSONObject JSONArray使用备忘
- JSONObject和JSONArray使用
- jsonObject、jsonArray的使用
- JSONObject 和JSONArray 方法测试
- JsonArray和JsonObject遍历方法
- JSONArray和JSONObject的使用
- JSONObject与JSONArray的使用
- 使用json-lib JSONObject JSONArray
- JSONObject与JSONArray的使用
- JSONObject与JSONArray的使用
- CRichEditCtrl 与 CEdit 不同之处
- [bzoj1010]:[HNOI2008]玩具装箱toy
- 各种加密算法简介及其Java实现
- git命令大全(非常齐全)
- 放弃去FBI的机会,在美做了2年数据研究工作,如今回国创业,他把全球最顶级的智能预测算法带到了国内
- JSONObject、JSONArray方法使用详解
- 大数据24小时:亚马逊AWS推出机密云服务,猎豹移动CTO范承工宣布离职
- MySQL嵌套事务的讨论
- 女神推荐, 卡片,广告图 ,点击查看更多
- centos7测试mysql c api
- 产品智能推荐规则,能否再聪明一些
- 解析线程池中任务队列(BlockingQueue)的分类
- curl code ERROR 60错误解决办法
- OpenGL ES的函数解析和记录理解