JsonArray和JsonObject的使用
import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class JsonTest { public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "huangwuyi");
jsonObject.put("sex", "男");
jsonObject.put("QQ", "413425430");
jsonObject.put("Min.score", new Integer(99));
jsonObject.put("nickname", "梦中心境");
// 输出jsonobject对象
System.out.println("=====jsonObject:" + jsonObject); // 判读输出对象的类型
boolean isArray = jsonObject.isArray();
boolean isEmpty = jsonObject.isEmpty();
boolean isNullObject = jsonObject.isNullObject();
System.out.println("=====是否为数组:" + isArray + ", 是否为空:" + isEmpty
+ "。 isNullObject:" + isNullObject); // 加入属性,在jsonObject后面追加元素。
jsonObject.element("address", "福建省厦门市");
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);
// 在jsonObject后面住家一个jsonArray
JSONArray array = jsonObject.getJSONArray("jsonArray");
System.out.println(jsonObject); System.out.println("=====返回一个JSONArray对象:" + array);
// 加入JSONArray后的值
// {"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"梦中心境","address":"福建省厦门市","jsonArray":["this is a jsonArray value","another jsonArray value"]}
System.out.println("=====结果:" + jsonObject); // 依据key返回一个字符串
String username = jsonObject.getString("username");
System.out.println("=====username:" + username); // 把字符转换为 JSONObject
String temp = jsonObject.toString();
JSONObject object = JSONObject.fromObject(temp);
// 转换后依据Key返回值
System.out.println("=====qq:" + object.get("QQ")); }
}
输出结果:
=====jsonObject:{"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"梦中心境"}
=====是否为数组:false。 是否为空:false。 isNullObject:false
=====加入属性后的对象:{"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"梦中心境","address":"福建省厦门市"}
{"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"梦中心境","address":"福建省厦门市","jsonArray":["this is a jsonArray value","another jsonArray value"]}
=====返回一个JSONArray对象:["this is a jsonArray value","another jsonArray value"]
=====结果:{"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"梦中心境","address":"福建省厦门市","jsonArray":["this is a jsonArray value","another jsonArray value"]}
=====username:huangwuyi
=====qq:413425430
另外一个样例:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class JsonObjectTest { /**
* @param args
*/
public static void main(String[] args) {
JSONObject jsonObj0 = new JSONObject();
JSONObject jsonObj = new JSONObject();
JSONObject jsonObj2 = new JSONObject();
JSONObject jsonObj3 = new JSONObject();
JSONArray jsonArray = new JSONArray(); // 创建jsonObj0
jsonObj0.put("name0", "zhangsan");
jsonObj0.put("sex1", "female");
System.out.println("jsonObj0:" + jsonObj0); // 创建jsonObj1
jsonObj.put("name", "xuwei");
jsonObj.put("sex", "male");
System.out.println("jsonObj:" + jsonObj); // 创建jsonObj2,包括两个条目,条目内容分别为jsonObj0。jsonObj1
jsonObj2.put("item0", jsonObj0);
jsonObj2.put("item1", jsonObj);
System.out.println("jsonObj2:" + jsonObj2); // 创建jsonObj3,仅仅有一个条目,内容为jsonObj2
jsonObj3.element("j3", jsonObj2);
System.out.println("jsonObj3:" + jsonObj3); // 往JSONArray中加入JSONObject对象。 发现JSONArray跟JSONObject的差别就是JSONArray比JSONObject多中括号[]
jsonArray.add(jsonObj);
System.out.println("jsonArray:" + jsonArray); JSONObject jsonObj4 = new JSONObject();
jsonObj4.element("weather", jsonArray);
System.out.println("jsonObj4:" + jsonObj4); } }
输出结果:
jsonObj0:{"name0":"zhangsan","sex1":"female"}
jsonObj:{"name":"xuwei","sex":"male"}
jsonObj2:{"item0":{"name0":"zhangsan","sex1":"female"},"item1":{"name":"xuwei","sex":"male"}}
jsonObj3:{"j3":{"item0":{"name0":"zhangsan","sex1":"female"},"item1":{"name":"xuwei","sex":"male"}}}
jsonArray:[{"name":"xuwei","sex":"male"}]
jsonObj4:{"weather":[{"name":"xuwei","sex":"male"}]}
总结:
JsonArray的对象中有方括号,有add和element两种set方法。有opt和get两种get方法。JsonObject是以key : value的形式存在的,有put和element两种set方法。有opt和get两种get方法。
JsonArray和JsonObject的使用的更多相关文章
- Android开发将List转化为JsonArray和JsonObject
客户端需要将List<Object>转化为JsonArray和JsonObject的方法: 首先,List中的Object的属性需要是public: class Person { publ ...
- fastjson的JSONArray和JSONObject
转自: http://blog.csdn.net/tangerr/article/details/76217924 Fastjson是国内著名的电子商务互联网公司阿里巴巴内部开发的用于java后台处理 ...
- JSONArray和JSONObject的简单使用
一.为什么要使用JSONArray和JSONObject 1.后台 -->前台 能够把java对象和集合转化成json字符串格式,这样在前台的ajax方法中能够直接转化成json对象使用 ,从后 ...
- Json中判断是JSONArray还是JSONObject
聪明的人总是能想到别人会遇到的问题,提前给出解决方案. List propList = new ArrayList(); //装载数据的list JSONArray array= JSONArray. ...
- JSON ,JSONArray和JSONObject
和 XML 一样,JSON 也是基于纯文本的数据格式.由于 JSON 天生是为 JavaScript 准备的,因此,JSON 的数据格式非常简单,可以用 JSON 传输一个简单的 String,Num ...
- java中转换json方式(JSONArray,JSONObject),json解析
package com.yunos.tv.video.resource.controller.web; import java.util.ArrayList; import java.util.Has ...
- jsonarray和jsonobject
JSONArray ja = new JSONArray(); ja.put("11"); ja.put("22"); ja.put("33" ...
- JsonArray和JsonObject遍历方法
一:遍历JsonArray String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'}, ...
- jsonArray与 jsonObject区别与js取值
一.JSONObject和JSONArray的数据表示形式 JSONObject的数据是用 { } 来表示的, 例如: { "id" : "123", & ...
随机推荐
- WPF学习- 新建项目后自定义Main()[Type 'App' already defines a member called 'Main' with the same parameter types]
问题点: 在App.xaml.cs中自己添加Main方法,编译会出现如下报错: 错误 CS0111 类型“App”已定义了一个名为“Main”的具有相同参数类型的成员 错误 Type 'App' a ...
- freenas 系统可能存在的bug
1.portal 中ip端口显示有问题. 2.创建extend/target映射之后重启iscsi服务有的时候不能启动. 3.后台/usr /etc 重启系统会自动还原.
- PageOffice NET MVC下使用
1)下载官方demo http://www.zhuozhengsoft.com/dowm/ 2)选择此项下载 3)官方demo暂时还未修改支持42版本以上的谷歌浏览器 所以需要修改以下部分 /home ...
- Android自动化测试之Monkey
本来是做Web后端的,来公司实习变成微信小程序前端了,到这周变成Android APP测试人员了,也是微醺啊. 由于对手工测试终究是有些抵触,所有昨天小试了一下不用写代码的自动化压力测试,在此记下我的 ...
- MySql-count(*)与count(id)与count(字段)之间的执行结果和性能分析
在mysql数据库中,当我们需要统计数据的时候,一定会用到count()这个方法,那么count(值)里面的这个值,到底应该怎么选择呢!常见有3种选择,(*,数字,列名),分别列出它们的执行结果和性能 ...
- 零基础入门学习Python(21)--函数:lambda表达式
知识点 lambda 表达式 Python 允许使用lambda关键字创建匿名函数 lambda 函数怎么使用? 单个参数 >>> def add(x): return 2*x + ...
- 微信小程序 设置计时器(setInterval)、清除计时器(clearInterval)
1.wxml代码 <!--index.wxml--> <view class="container"> <button type='primary' ...
- node Buffer.byteLength()
Buffer.byteLength(string[, encoding]) string {String} | {Buffer} | {TypedArray} | {DataView} | {Arra ...
- python 连接sqlserver: pymssql
停了一个月,终于还是把这个做了,工作需要!!!在装pymssql时,一直报错,确定了要先装freetds: 1. 安装freetds时报错,搜索到要先进行如下操作: brew unlink freet ...
- pyton学习之路
文件操作 打开文件的模式有: r,只读模式(默认). w,只写模式.[不可读:不存在则创建:存在则删除内容:] a,追加模式.[可读: 不存在则创建:存在则只追加内容:] "+" ...