JSONObject与JSONArray的使用方法
来源:互联网 发布:muse cc mac 破解 编辑:程序博客网 时间:2024/04/29 03:08
JSONObject与JSONArray的使用方法
一、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()方法。
- package com.tjcyjd;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- public class JSONObjectSample {
- // 创建JSONObject对象
- private static JSONObject createJSONObject() {
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("username", "yangjinde");
- jsonObject.put("sex", "男");
- jsonObject.put("QQ", "908599713");
- jsonObject.put("yjd.score", new Integer(100));
- jsonObject.put("nickname", "搁浅");
- 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添加属性
- jsonObject.element("address", "北京中关村");
- System.out.println("添加属性后的对象:" + jsonObject);
- // 创建一个JSONArray对象
- JSONArray jsonArray = new JSONArray();
- jsonArray.add(0, "this is a jsonArray value");
- jsonArray.add(1, "this is another jsonArray value");
- // 往JSONObject添加JSONArray
- jsonObject.element("jsonArray", jsonArray);
- JSONArray array = jsonObject.getJSONArray("jsonArray");
- System.out.println("array:" + array);
- System.out.println("添加JSONArray后的对象:" + jsonObject);
- // 根据key返回一个字符串
- String username = jsonObject.getString("username");
- System.out.println("username:" + username);
- // 把字符转换为 JSONObject
- String temp = jsonObject.toString();
- System.out.println("要转换为JSONObject的字符串为:" + temp);
- JSONObject object = JSONObject.fromObject(temp);
- // 转换后根据Key返回值
- System.out.println("qq:" + object.get("QQ"));
- }
- }
运行的结果如下:
- jsonObject:{"username":"yangjinde","sex":"男","QQ":"908599713","yjd.score":100,"nickname":"搁浅"}
- isArray:false isEmpty:false isNullObject:false
- 添加属性后的对象:{"username":"yangjinde","sex":"男","QQ":"908599713","yjd.score":100,"nickname":"搁浅","address":"北京中关村"}
- array:["this is a jsonArray value","this is another jsonArray value"]
- 添加JSONArray后的对象:{"username":"yangjinde","sex":"男","QQ":"908599713","yjd.score":100,"nickname":"搁浅","address":"北京中关村","jsonArray":["this is a jsonArray value","this is another jsonArray value"]}
- username:yangjinde
- 要转换为JSONObject的字符串为:{"username":"yangjinde","sex":"男","QQ":"908599713","yjd.score":100,"nickname":"搁浅","address":"北京中关村","jsonArray":["this is a jsonArray value","this is another jsonArray value"]}
- qq:908599713
0 0
- JSONObject与JSONArray的使用方法
- JSONObject与JSONArray的使用方法 .
- JSONObject与JSONArray的使用方法
- java中 JSONObject与JSONArray的使用方法
- JSONObject和JSONArray的使用方法
- JSONObject与JSONArray的使用
- JSONObject与JSONArray的使用
- JSONObject与JSONArray的使用
- JSONObject与JSONArray的使用
- JSONObject与JSONArray的使用
- JSONObject与JSONArray的使用
- JSONObject与JSONArray的使用
- JSONObject与JSONArray的使用
- JSONObject与JSONArray的使用
- JSONObject与JSONArray的使用
- JsonObject 与JsonArray的使用
- JSONObject与JSONArray的使用
- JSONObject与JSONArray的使用
- java中substring和indexof() 和lastindexof()
- 大端小端的介绍
- 基于CSM和PCF的软阴影实现
- 查看tidal job中调用的sp是否真的在运行
- error LNK2001: unresolved external symbol __endthreadex,unresolved external symbol __endthreadex
- JSONObject与JSONArray的使用方法
- base64编码转码
- iOS获取设备唯一标识符
- ubuntu 14.10 系统 下 以太网 “线缆被拔出” 问题 开机自动一次性解决!
- Shader Model 5 的新增属性
- activeMQ消费消息时网络流量异常大的问题 .
- 用SecureCRT在windows和CentOS间上传下载文件
- Web项目报错java.lang.ClassCastException
- Nginx反向代理和负载均衡部署指南