002_JSONObject、JSONArray和JsonConfig对象简单使用

1. JSONObject对象

1.1. JSONObject是一个无序的键值对集合。它的外部文本形式是一个用大括号括起来的字符串, 使用冒号分割键和值, 并且是用逗号分隔多个值。

1.2. JSONObject可以添加boolean、int、long、Double、String、JSONNull、JSONObject、JSONArray、Object。

1.3. JSONObject可以把JSONObject对象转换为Java对象, 也可以把Map、JavaBean对象转换为JSONObject对象。

2. JSONArray对象

2.1. JSONArray是值的有序序列。它的外部文本形式是一个用方括号括起来的字符串, 并且是用逗号分隔多个值。

2.2. JSONArray可以添加boolean、int、long、Double、String、JSONNull、JSONObject、JSONArray、Object。

2.3. JSONArray可以把JSONArray对象转换为Java数组或Java集合, 也可以把Java对象转换为JSONArray对象。

3. JsonConfig对象

3.1. 无论出于何种原因, 某些时候, 我们需要对JSON字符串和Java对象互相转换的过程加以控制, 最常见需求如数值格式化和日期格式化。Json-lib提供了JsonConfig对象, 该对象能够深刻影响Java对象和JSON字符串互相转换的行为。

4. toString()方法

4.1. String toString(), 获取JSONObject、JSONArray的JSON文本。

5. isEmpty()方法

5.1. boolean isEmpty(), 判断JSONObject是否有key, 如果JSONObject没有key返回true, 反之返回false。

5.2. boolean isEmpty(), 判断JSONArray是否有元素, 如果JSONArray没有元素返回true, 反之返回false。

6. isArray()方法

6.1. boolean isArray(), 判断JSONObject、JSONArray对象是否是JSONArray, 如果是返回true, 反之返回false。

7. clear()方法

7.1. void clear(), 清空JSONObject、JSONArray对象。

8. size()方法

8.1. int size(), 获得JSONObject储存key的数量。

8.2. int size(), 获得JSONArray储存元素的数量。

9. JsonConfig对象

9.1. setRootClass(Class rootClass)设置JsonConfig当前的root Class。JSON对象转Java对象的时候, JSON对象的值匹配这个root Class的类型。

9.2. setClassMap(Map classMap)设置JsonConfig当前的Class Map。JSON对象转Java对象的时候, JSON对象里面还包含JSON对象数据, 可以根据这数据的key, 在Class Map里获取key对应的值的类型。

9.3. getRootClass()获得JsonConfig当前的root Class。

9.4. getClassMap()获得JsonConfig当前的Class Map。

10. 例子

10.1. 新建一个名为JSONObjectArrayConfig的Java项目, 同时添加相关jar包。

10.2. 新建ToString.java

package com.fj.oac;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * String toString(), 获取JSONObject、JSONArray的JSON文本。
 */
public class ToString {
	public static void main(String[] args) {
		// 创建一个没有任何值的JSONObject类型的jsonObject对象
		JSONObject jsonObject = new JSONObject();
		jsonObject.accumulate("name", "HUAWEI P40 Pro"); // 给jsonObject对象累积值

		// 创建一个没有任何值的JSONArray类型的jsonArray对象
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(true); // 给jsonArray对象添加值
		jsonArray.add(5988); // 给jsonArray对象添加值
		
		System.out.println(jsonObject.toString()); // 获取jsonArray对象的JSON文本。
		System.out.println(jsonArray.toString()); // 获取jsonArray对象的JSON文本。
	}
}

10.3. 运行ToString.java

10.4. 新建IsEmpty.java

package com.fj.oac;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * boolean isEmpty(), 判断JSONObject是否有key, 如果JSONObject没有key返回true, 反之返回false。
 * boolean isEmpty(), 判断JSONArray是否有元素, 如果JSONArray没有元素返回true, 反之返回false。
 */
public class IsEmpty {
	public static void main(String[] args) {
		// 创建一个没有任何值的JSONObject类型的jsonObject对象
		JSONObject jsonObject = new JSONObject();
		jsonObject.accumulate("name", "HUAWEI P40 Pro"); // 给jsonObject对象累积值

		// 创建一个没有任何值的JSONArray类型的jsonArray对象
		JSONArray jsonArray = new JSONArray();
		
		System.out.println("jsonObject是否为空: " + jsonObject.isEmpty()); // 判断jsonObject对象是否有元素
		System.out.println("jsonArray是否为空: " + jsonArray.isEmpty()); // 判断jsonArray对象是否有元素
	}
}

10.5. 运行IsEmpty.java

10.6. 新建IsArray.java

package com.fj.oac;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * boolean isArray(), 判断JSONObject、JSONArray对象是否是JSONArray, 如果是返回true, 反之返回false。
 */
public class IsArray {
	public static void main(String[] args) {
		// 创建一个没有任何值的JSONObject类型的jsonObject对象
		JSONObject jsonObject = new JSONObject();
		jsonObject.accumulate("name", "HUAWEI P40 Pro"); // 给jsonObject对象累积值

		// 创建一个没有任何值的JSONArray类型的jsonArray对象
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(true); // 给jsonArray对象添加值
		jsonArray.add(5988); // 给jsonArray对象添加值
		
		System.out.println("jsonObject是否是数组类型: " + jsonObject.isArray()); // 判断jsonObject对象是否是JSONArray
		System.out.println("jsonArray是否是数组类型: " + jsonArray.isArray()); // 判断jsonArray对象是否是JSONArray
	}
}

10.7. 运行IsArray.java

10.8. 新建Clear.java

package com.fj.oac;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * void clear(), 清空JSONObject、JSONArray对象。
 */
public class Clear {
	public static void main(String[] args) {
		// 创建一个没有任何值的JSONObject类型的jsonObject对象
		JSONObject jsonObject = new JSONObject();
		jsonObject.accumulate("name", "HUAWEI P40 Pro"); // 给jsonObject对象累积值

		// 创建一个没有任何值的JSONArray类型的jsonArray对象
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(true); // 给jsonArray对象添加值
		jsonArray.add(5988); // 给jsonArray对象添加值
		
		System.out.println("清空前:");
		System.out.println(jsonObject.toString());
		System.out.println(jsonArray.toString());
		
		jsonObject.clear(); // 清空jsonObject
		jsonArray.clear(); // 清空jsonArray
		
		System.out.println("清空后:");
		System.out.println(jsonObject.toString());
		System.out.println(jsonArray.toString());
	}
}

10.9. 运行Clear.java

10.10. 新建Size.java

package com.fj.oac;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * int size(), 获得JSONObject储存key的数量。
 * int size(), 获得JSONArray储存元素的数量。
 */
public class Size {
	public static void main(String[] args) {
		// 创建一个没有任何值的JSONObject类型的jsonObject对象
		JSONObject jsonObject = new JSONObject();
		jsonObject.accumulate("name", "HUAWEI P40 Pro"); // 给jsonObject对象累积值

		// 创建一个没有任何值的JSONArray类型的jsonArray对象
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(true); // 给jsonArray对象添加值
		jsonArray.add(5988); // 给jsonArray对象添加值
		
		System.out.println("jsonObject储存key的数量: " + jsonObject.size()); // 获得jsonObject储存key的数量。
		System.out.println("jsonArray储存元素的数量: " + jsonArray.size()); // 获得jsonArray储存元素的数量。
	}
}

10.11. 运行Size.java

10.12. 新建Sensor.java

package com.fj.oac;

import java.io.Serializable;

/**
 * 传感器
 */
public class Sensor implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private Boolean weightSensor; // 重力传感器
	private Boolean fingerSensor; // 指纹传感器
	private String otherSensor; // 其它传感器
	
	public Sensor() {}

	public Sensor(Boolean weightSensor, Boolean fingerSensor, String otherSensor) {
		this.weightSensor = weightSensor;
		this.fingerSensor = fingerSensor;
		this.otherSensor = otherSensor;
	}

	public Boolean getWeightSensor() {
		return weightSensor;
	}

	public void setWeightSensor(Boolean weightSensor) {
		this.weightSensor = weightSensor;
	}

	public Boolean getFingerSensor() {
		return fingerSensor;
	}

	public void setFingerSensor(Boolean fingerSensor) {
		this.fingerSensor = fingerSensor;
	}

	public String getOtherSensor() {
		return otherSensor;
	}

	public void setOtherSensor(String otherSensor) {
		this.otherSensor = otherSensor;
	}

	@Override
	public String toString() {
		return "{weightSensor=" + weightSensor + ", fingerSensor=" + fingerSensor + ", otherSensor=" + otherSensor + "}";
	}
}

10.13. 新建JSONConfigRootClassClassMap.java

package com.fj.oac;

import java.util.HashMap;
import java.util.Map;
import net.sf.json.JsonConfig;

/**
 * JsonConfig
 * 1. setRootClass(Class rootClass)设置JsonConfig当前的root Class。JSON对象转Java对象的时候, JSON对象的值匹配这个root Class的类型。
 * 2. setClassMap(Map classMap)设置JsonConfig当前的Class Map。JSON对象转Java对象的时候, JSON对象里面还包含JSON对象数据, 可以根据这数据的key, 在Class Map里获取key对应的值的类型。
 * 3. getRootClass()获得JsonConfig当前的root Class。
 * 4. getClassMap()获得JsonConfig当前的Class Map。
 */
public class JSONConfigRootClassClassMap {
	public static void main(String[] args) {
		Map<String, Class<?>> classMap = new HashMap<>();
		classMap.put("map", Map.class);
		classMap.put("sensor", Sensor.class);
		
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setRootClass(Map.class);
		jsonConfig.setClassMap(classMap);
		
		System.out.println("root class: " + jsonConfig.getRootClass());
		System.out.println("class map: " + jsonConfig.getClassMap());
	}
}

10.14. 运行JSONConfigRootClassClassMap.java

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值