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 ...
随机推荐
- dp入门题目
本文文旨,如题... 转载请注明出处... HDOJ 1176 免费馅饼 http://acm.hdu.edu.cn/showproblem.php?pid=1176 类似数塔,从底往上推,每次都是从 ...
- USACO 4.3 Letter Game (字典树)
Letter GameIOI 1995 Figure 1: Each of the 26 lowercase letters and its value Letter games are popula ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) F - Cowslip Collections 数论 + 容斥
F - Cowslip Collections http://codeforces.com/blog/entry/43868 这个题解讲的很好... #include<bits/stdc++.h ...
- 009 HDFS的shell命令(里面有一个安全模式)
一:dfs的命令 1.所有的领命 2.解释 -appendToFile <localsrc> ... <dst> :追加文件 -checksum <src> . ...
- [CodeForces 893D] Credit Card 贪心
题意: Recenlty Luba有一张信用卡,一开始金额为0,每天早上可以充值任意数量的钱,但有限制,卡里的钱不能超过D.到了晚上,银行会对信用卡进行一次操作,操作有三种: 1.a[i]>0, ...
- MSTP多生成树的配置
STP的不足 STP协议虽然能够解决环路问题,但是由于网络拓扑收敛较慢,影响了用户通信质量 而且如果网络中的拓扑结构频繁变化,网络也会随之频繁失去连通性,从而导致用户通信频繁中断 RSTP对STP的改 ...
- ARM 常用汇编指令
ARM 汇编程序的框架结构 .section .data <初始化的数据> .section.bss <未初始化的数据> .section .text .global _sta ...
- softmax为什么使用指数函数?(最大熵模型的理解)
解释1: 他的假设服从指数分布族 解释2: 最大熵模型,即softmax分类是最大熵模型的结果. 关于最大熵模型,网上很多介绍: 在已知部分知识的前提下,关于未知分布最合理的推断就是符合已知知识最不确 ...
- 【Codeforces 498B】 B. Name That Tune (概率DP)
B. Name That Tune time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- android studio 汉化包 美化包
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...