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的使用的更多相关文章

  1. Android开发将List转化为JsonArray和JsonObject

    客户端需要将List<Object>转化为JsonArray和JsonObject的方法: 首先,List中的Object的属性需要是public: class Person { publ ...

  2. fastjson的JSONArray和JSONObject

    转自: http://blog.csdn.net/tangerr/article/details/76217924 Fastjson是国内著名的电子商务互联网公司阿里巴巴内部开发的用于java后台处理 ...

  3. JSONArray和JSONObject的简单使用

    一.为什么要使用JSONArray和JSONObject 1.后台 -->前台 能够把java对象和集合转化成json字符串格式,这样在前台的ajax方法中能够直接转化成json对象使用 ,从后 ...

  4. Json中判断是JSONArray还是JSONObject

    聪明的人总是能想到别人会遇到的问题,提前给出解决方案. List propList = new ArrayList(); //装载数据的list JSONArray array= JSONArray. ...

  5. JSON ,JSONArray和JSONObject

    和 XML 一样,JSON 也是基于纯文本的数据格式.由于 JSON 天生是为 JavaScript 准备的,因此,JSON 的数据格式非常简单,可以用 JSON 传输一个简单的 String,Num ...

  6. java中转换json方式(JSONArray,JSONObject),json解析

    package com.yunos.tv.video.resource.controller.web; import java.util.ArrayList; import java.util.Has ...

  7. jsonarray和jsonobject

    JSONArray ja = new JSONArray(); ja.put("11"); ja.put("22"); ja.put("33" ...

  8. JsonArray和JsonObject遍历方法

    一:遍历JsonArray String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'}, ...

  9. jsonArray与 jsonObject区别与js取值

    一.JSONObject和JSONArray的数据表示形式 JSONObject的数据是用 {  } 来表示的, 例如:   { "id" : "123", & ...

随机推荐

  1. react link引入外部css样式的坑

    刚开始的代码是这样的,使用react router4.x写的demo路由跳转后,页面的没有渲染,是因为没有引入外部css文件(或者说引入外部文件路径错误) <!DOCTYPE html> ...

  2. Poi 写入图片进入excel

    public static void cacheWritePicture(BufferedImage bufferImg, Sheet sheet, Workbook wb, int width, i ...

  3. 01Oracle Database

    Oracle Database Oracle Database又名Oracle RDBMS简称Oracle是甲骨文公司的一款关系数据库管理系统. Oracle Website.

  4. 【C语言】控制台窗口图形界面编程(七):鼠标事件

    目录 00. 目录 01. INPUT_RECORD结构 02. MOUSE_EVENT_RECORD结构 03. ReadConsoleInput函数 04. 示例程序 00. 目录 01. INP ...

  5. JAVA基础——设计模式之简单工厂模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述简单工厂模式的:简单工厂模式是类的创建模式,又叫做静态工厂方法(Static Factory Method)模式.简单工厂模式是由一个工厂 ...

  6. vue-cli webpack 快速搭建项目

    一.安装vue npm install vue -g 二.用vue-cli快速搭建项目 //全局安装vue-cli npm install install -g vue-cli //创建一个基于web ...

  7. KBE_创建项目和基本常识

    此笔记参考官方文档 第一个项目 资产库:是每一个项目文件夹的名称,使用KBE提供的生成工具生成一个最小资产库,其中包含了很多常用的工具,默认名server_assets: res:放置一些资源(入地图 ...

  8. MySQL主从复制遇到的问题

    show slave status\G时,io显示:Slave_IO_State: Connecting 1.检查网络是否畅通,方法:ping主机ip.主机通畅. 2.检查复制用户的账号密码是否正确. ...

  9. [Python3网络爬虫开发实战] 3.2.1-基本用法

    1. 准备工作 在开始之前,请确保已经正确安装好了requests库.如果没有安装,可以参考1.2.1节安装. 2. 实例引入 urllib库中的urlopen()方法实际上是以GET方式请求网页,而 ...

  10. 配置Mysql审计

    mysql-audit.json:Mysql审计日志 插件下载地址: https://bintray.com/mcafee/mysql-audit-plugin/release/1.1.4-725#f ...