JSONObject以及json(转)
一、JAR包简介
要使程序 可以运行 必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包:
1.commons-lang.jar
2.commons-beanutils.jar
3.commons-collections.jar
4.commons-logging.jar
5.ezmorph.jar
6.json-lib-2.2.2-jdk15.jar
二、JSONObject对象 使用
JSON-lib包是一个beans,collections,maps,java arrays 和XML和JSON互相转换的包。在本例中,我们将使用JSONObject类创建JSONObject对象,然后我们打印这些对象的值。为了使用 JSONObject对象,我们要引入"net.sf.json"包。为了给对象添加元素,我们要使用put()方法。
import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class JSONObjectSample { //创建JSONObject对象
private static JSONObject createJSONObject(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "kevin");
jsonObject.put("Max.score", new Integer(100));
jsonObject.put("Min.score", new Integer(50));
jsonObject.put("nickname", "picglet");
return jsonObject;
}
public static void main(String[] args) {
JSONObject jsonObject = JSONObjectSample.createJSONObject();
//输出jsonobject对象
System.out.println("jsonObject==>"+jsonObject); //判读输出对象的类型
boolean isArray = jsonObject.isArray();
boolean isEmpty = jsonObject.isEmpty();
boolean isNullObject = jsonObject.isNullObject();
System.out.println("isArray:"+isArray+" isEmpty:"+isEmpty+" isNullObject:"+isNullObject); //添加属性
jsonObject.element("address", "swap lake");
System.out.println("添加属性后的对象==>"+jsonObject); //返回一个JSONArray对象
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "this is a jsonArray value");
jsonArray.add(1,"another jsonArray value");
jsonObject.element("jsonArray", jsonArray);
JSONArray array = jsonObject.getJSONArray("jsonArray");
System.out.println("返回一个JSONArray对象:"+array);
//添加JSONArray后的值
//{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake",
//"jsonArray":["this is a jsonArray value","another jsonArray value"]}
System.out.println(jsonObject); //根据key返回一个字符串
String jsonString = jsonObject.getString("name");
System.out.println("jsonString==>"+jsonString);
}
} import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class JSONObjectSample { //创建JSONObject对象
private static JSONObject createJSONObject(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "kevin");
jsonObject.put("Max.score", new Integer(100));
jsonObject.put("Min.score", new Integer(50));
jsonObject.put("nickname", "picglet");
return jsonObject;
}
public static void main(String[] args) {
JSONObject jsonObject = JSONObjectSample.createJSONObject();
//输出jsonobject对象
System.out.println("jsonObject==>"+jsonObject); //判读输出对象的类型
boolean isArray = jsonObject.isArray();
boolean isEmpty = jsonObject.isEmpty();
boolean isNullObject = jsonObject.isNullObject();
System.out.println("isArray:"+isArray+" isEmpty:"+isEmpty+" isNullObject:"+isNullObject); //添加属性
jsonObject.element("address", "swap lake");
System.out.println("添加属性后的对象==>"+jsonObject); //返回一个JSONArray对象
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "this is a jsonArray value");
jsonArray.add(1,"another jsonArray value");
jsonObject.element("jsonArray", jsonArray);
JSONArray array = jsonObject.getJSONArray("jsonArray");
System.out.println("返回一个JSONArray对象:"+array);
//添加JSONArray后的值
//{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake",
//"jsonArray":["this is a jsonArray value","another jsonArray value"]}
System.out.println(jsonObject); //根据key返回一个字符串
String jsonString = jsonObject.getString("name");
System.out.println("jsonString==>"+jsonString);
}
}
得到JSONObject对象后我们就可以使用它的方法了,可以查看其API,我给出一个在线 的API
http://json-lib.sourceforge.net/apidocs/jdk15/index.html
Html代码
JSONArray的方法测试可以类似进行
JSONArray的方法测试可以类似进行
JSONObject以及json(转)的更多相关文章
- 在JAVA中使用JSONObject生成json
JSON是一种轻量级的数据交换格式,在现在的web开发中,是非常常见的.在没有方便的工具之前,我们或许会使用拼字符串的形式来生成json数组,今天我们使用一个json-lib.jar包来为我们实现生成 ...
- JSONObject 转换 JSON复杂对象
Bean定义: public class GetM100DataResponse { private String service;//接口代码 private String sessionId;// ...
- JSONObject解析json数据
首先先看一下我们要解析的json数据是什么样子的: 代码: String url="http://113.57.190.228:8001/Web/Report/GetBigMSKReport ...
- 使用JsonObject解析json
第一种: [ { "0": "1", "1": "一", "id": "1", ...
- Android使用自带JSONObject解析JSON数据
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android ...
- JSONObject转换JSON之将Date转换为指定格式(转)
项目中,经常会用JSONObject插件将JavaBean或List<JavaBean>转换为JSON格式的字符串,而JavaBean的属性有时候会有java.util.Date这个类型的 ...
- 【Unity】使用JSONObject解析Json
为何要用JSONObject 之前已经用过JsonUtility和Newton.Json来解析Json了,为什么现在又要用一个新的JSONObject来解析Json? 使用JsonUtility:ht ...
- JSONObject JSONArray json字符串 HashMap ArryList 在java开发中用到的数据结构
1.JSONObject 长成这样的: { "key1":value1, "key2":value2, "key3":value3} ...
- 浅谈JSONObject解析JSON数据
我们在做jmeter接口测试时能会用beanshell断言,一般都会将返回值转成JSONObject对象进行处理.本文选取较为复杂json格式数据,也将适用于java接口测试. JSON数据 { &q ...
随机推荐
- Linux学习笔记:ls和ll命令
list显示当前目录中的文件名字,不加参数时显示除隐藏文件外的所有文件及目录的名字. ll 等同于 ls -l-r 对目录反向排序(按字母)-t 以时间排序-u 以文件上次被访问的时间排序-x 按列输 ...
- Elasticsearch5.0 安装问题
使用Elasticsearch5.0 必须安装jdk1.8 [elsearch@vm-mysteel-dc-search01 bin]$ java -version java version &quo ...
- RAII
转载:http://www.jellythink.com/archives/101 什么是RAII? RAII是Resource Acquisition Is Initialization的简称,是C ...
- C语言:凯撒密码的实现
凯撒密码的实现(10分)题目内容: 凯撒密码(caeser)是罗马扩张时期朱利斯?凯撒(Julius Caesar)创造的,用于加密通过信使传递的作战命令.它将字母表中的字母移动一定位置而实现加密. ...
- 8-10 Coping Books uva714
题意:把一个包含m个正整数的序列划分为k个 1<=k<=m<=500的非空连续子序列 使得每个正整数恰好属于一个序列 设第i个序列的各个数之和为 Si 你的任务是让所有的 ...
- spring中的scope详解
spring容器中的bean默认是单例模式的,改成非单例模式需要在类上加上@Scope("prototype") 1. scope概论 spring中scope是一个非常关键的概念 ...
- HTTP 缓存之ETag 和Cache-Control的使用方法介绍
什么是http缓存,有什么作用 通过网络获取内容既速度缓慢又开销巨大.较大的响应需要在客户端与服务器之间进行多次往返通信,这会延迟浏览器获得和处理内容的时间,还会增加访问者的流量费用.因此,缓存并重复 ...
- iOS Sprite Kit教程之编写程序以及Xcode的介绍
iOS Sprite Kit教程之编写程序以及Xcode的介绍 Xcode界面介绍 一个Xcode项目由很多的文件组成,例如代码文件.资源文件等.Xcode会帮助开发者对这些文件进行管理.所以,Xco ...
- 【CF398B】B. Painting The Wall(期望)
B. Painting The Wall time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [xsy2913]enos
题意:一棵树,点有$0,1,2$三种颜色,支持路径修改颜色和查询点所在同色连通块的大小 lcm太可怕了,于是去问了sk,得到一个优质做法 考虑lct维护子树信息,$vs_{x,i}$为$x$的虚儿子中 ...