007_JSONObject对象静态方法

1. JSONObject.fromObject静态方法

1.1. JSONObject JSONObject.fromObject(Object object)静态方法, 把Map、JavaBean、JSON Object String对象转换为JSONObject对象。json-lib底层, 会创建一个JsonConfig对象使用。不能转换数组、集合, 需要用JSONArray转换。

1.2. JSONObject JSONObject.fromObject(Object object, JsonConfig jsonConfig)静态方法, 把Map、JavaBean、JSON Object String对象转换为JSONObject对象。并指定一个JsonConfig。

1.3. 新建Sensor.java

package com.fj.jo;

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 + "}";
	}
}

1.4. 新建JSONObjectStaticFromObject.java

package com.fj.jo;

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

/**
 * JSONObject
 * 1. JSONObject JSONObject.fromObject(Object object)静态方法, 把Map、JavaBean、JSON Object String对象转换为JSONObject对象。
 * json-lib底层, 会创建一个JsonConfig对象使用。不能转换数组、集合, 需要用JSONArray转换。
 * 2. JSONObject JSONObject.fromObject(Object object, JsonConfig jsonConfig)静态方法, 把Map、JavaBean、JSON Object String对象转换为JSONObject对象。并指定一个JsonConfig。
 */
public class JSONObjectStaticFromObject {
	public static void main(String[] args) {
		// Map
		Map<String, String> mainParam = new HashMap<String, String>();
		mainParam.put("runtimeMemory", "8GB");
		mainParam.put("mobileMemory", "128GB");
		mainParam.put("CPUType", "HUAWEI Kirin 990 5G(麒麟990 5G)");
		mainParam.put("CPUCount", "八核");
		// Java Bean
		Sensor sensor = new Sensor();
		sensor.setWeightSensor(true);
		sensor.setFingerSensor(true);
		sensor.setOtherSensor("姿态感应器、色温传感器");

		String jsonObjectStr = "{\"name\":\"HUAWEI P40 Pro\",\"support5G\":true,\"price\":\"2988\",\"productCode\":2601010188703,\"screenSize\":6.58,\"mobileColor\":[\"亮黑色\",\"冰霜银\",\"零度白\",\"深海蓝\",\"晨曦金\"],\"isp\":[\"移动\",\"联通\",\"电信\"],\"mainParam\":{\"mobileMemory\":\"128GB\",\"CPUCount\":\"八核\",\"CPUType\":\"HUAWEI Kirin 990 5G(麒麟990 5G)\",\"runtimeMemory\":\"8GB\"},\"sensor\":{\"otherSensor\":\"姿态感应器、色温传感器\",\"weightSensor\":true,\"fingerSensor\":true},\"netJsonObject\":{\"5G\":\"移动5G(NR)/联通5G(NR)/电信5G(NR)\",\"4G\":\"移动/联通/电信4G主副卡 支持 TD-LTE/LTE FDD\",\"3G\":\"主卡: 联通3G(WCDMA)/电信3G(CDMA 2000); 副卡: 联通3G(WCDMA)\",\"2G\":\"主卡: 移动2G(GSM)/联通2G(GSM)/电信2G(CDMA 1X); 副卡: 移动2G(GSM)/联通2G(GSM)/电信2G(CDMA 1X)\"},\"transferFunction\":[\"NFC\",\"WLAN\"]}";
		
		// 把Map对象转换为JSONObject对象
		JSONObject mapJSONObject = JSONObject.fromObject(mainParam); 
		System.out.println(mapJSONObject.toString()); 
		
		// 把JavaBean对象转换为JSONObject对象
		JSONObject javaBeanJSONObject = JSONObject.fromObject(sensor, new JsonConfig()); 
		System.out.println(javaBeanJSONObject.toString());
		
		// 把JSON Object String对象转换为JSONObject对象
		JSONObject strJSONObject = JSONObject.fromObject(jsonObjectStr);
		System.out.println(strJSONObject);
	}
}

1.5. 运行JSONObjectStaticFromObject.java

2. JSONObject.toBean静态方法

2.1. Object JSONObject.toBean(JSONObject jsonObject, Class beanClass)静态方法, 把JSONObject对象转换为Java对象。会把第二个参数beanClass设置为JSONConfig的root Class, 并根据root Class类型创建对象, 然后把JSON对象数据封装到创建好的对象。

2.2. Object JSONObject.toBean(JSONObject jsonObject, Class beanClass, Map classMap)静态方法, 把JSONObject对象转换为Java对象。第二个参数beanClass会设置为JsonConfig的root Class, 第三个参数classMap会设置为JsonConfig的Class Map。

2.3. Object JSONObject.toBean(JSONObject jsonObject, JsonConfig jsonConfig)静态方法, 把JSONObject对象转换为Java对象。会把第二个参数传入一个JsonConfig, JSONConfig可以设置root Class、Class Map。

2.4. Object JSONObject.toBean(JSONObject jsonObject, Object root, JsonConfig jsonConfig)静态方法, 把JSONObject对象转换为Java对象。第二个参数传入一个root对象, JSONObject根据root对象的类型, 创建新对象, 然后把数据封装到对象里。第三个参数JsonConfig这个时候设置的root Class, 没有用了, Class Map依然匹配JSONObject对象内部JSON对象数据。

2.5. 会把JSONObject对象的boolean、int、long、Double、String、JSONNull、JSONArray类型的数据封装root Class里面, root Class推荐使用Map.class。

2.6. JSONObject对象内部JSON对象数据可以根据键匹配Class Map的key, 获得key相对应的类型, 然后把数据封装到这个类型的对象里。

2.7. 新建JSONObjectStaticToBeanBeanClass.java(使用JSONObject.toBean(JSONObject jsonObject, Class beanClass)方法)

package com.fj.jo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;

/**
 * JSONObject
 * 1. Object JSONObject.toBean(JSONObject jsonObject, Class beanClass)静态方法, 把JSONObject对象转换为Java对象。
 * 会把第二个参数beanClass设置为JSONConfig的root Class, 并根据root Class类型创建对象, 然后把JSON对象数据封装到创建好的对象。
 * 2. JSONObject是一种键值对集合, 所以传入Map.class或者JavaBean对象是一个好主意。
 */
public class JSONObjectStaticToBeanBeanClass {
	public static void main(String[] args) {
		// 数组
		String[] mobileColor = {"亮黑色", "冰霜银", "零度白", "深海蓝", "晨曦金"};
		// 集合
		List<String> isp = new ArrayList<String>(); // ISP网络服务提供商
		isp.add("移动");
		isp.add("联通");
		isp.add("电信");
		// Map
		Map<String, String> mainParam = new HashMap<String, String>();
		mainParam.put("runtimeMemory", "8GB");
		mainParam.put("mobileMemory", "128GB");
		mainParam.put("CPUType", "HUAWEI Kirin 990 5G(麒麟990 5G)");
		mainParam.put("CPUCount", "八核");
		// Java Bean
		Sensor sensor = new Sensor();
		sensor.setWeightSensor(true);
		sensor.setFingerSensor(true);
		sensor.setOtherSensor("姿态感应器、色温传感器");

		JSONObject jsonObject = new JSONObject();
		jsonObject.element("name", "HUAWEI P40 Pro")
		.element("support5G", true)
		.element("price", 5988)
		.element("productCode", 2601010188703L)
		.element("screenSize", 6.58D)
		.element("jsonNull", JSONNull.getInstance())
		.element("mobileColor", mobileColor)
		.element("isp", isp)
		.element("mainParam", mainParam)
		.element("sensor", sensor);
	
		mapBeanClass(jsonObject);
		javaBeanClass(jsonObject.optJSONObject("sensor"));
	}

	/** 
	 * 复杂的JSONObject对象, 传入Map.class对象。
	 * 会把JSONObject复杂对象的boolean、int、long、Double、String、JSONNull、JSONArray类型的数据封装HashMap里面。
	 * JSONObject对象内部的JSON对象数据封装到了DynaBean动态Bean里。
	 * 
	 * @param jsonObject
	 */
	public static void mapBeanClass(JSONObject jsonObject) {
		Object obj = JSONObject.toBean(jsonObject, Map.class);
		System.out.println(obj);
	}
	
	/**
	 * 传入Sensor.class对象, 把这个单一的JSONObject对象封装到Sensor对象里。
	 * @param jsonObject
	 */
	public static void javaBeanClass(JSONObject jsonObject) {
		Sensor sensor = (Sensor)JSONObject.toBean(jsonObject, Sensor.class);
		System.out.println(sensor);
	}
	
}

2.8. 运行JSONObjectStaticToBeanBeanClass.java

2.9. 新建JSONObjectStaticToBeanBeanClassClassMap.java(使用JSONObject.toBean(JSONObject jsonObject, Class beanClass, Map classMap)方法)

package com.fj.jo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;

/**
 * JSONObject
 * 1. Object JSONObject.toBean(JSONObject jsonObject, Class beanClass, Map classMap)静态方法, 把JSONObject对象转换为Java对象。
 * 第二个参数beanClass会设置为JsonConfig的root Class, 第三个参数classMap会设置为JsonConfig的Class Map。
 * 2. 会把JSONObject对象的boolean、int、long、Double、String、JSONNull、JSONArray类型的数据封装root Class里面, root Class推荐使用Map.class。
 * 3. JSONObject对象内部JSON对象数据可以根据键匹配Class Map的key, 获得key相对应的类型, 然后把数据封装到这个类型的对象里。
 */
public class JSONObjectStaticToBeanBeanClassClassMap {
	public static void main(String[] args) {
		// 数组
		String[] mobileColor = {"亮黑色", "冰霜银", "零度白", "深海蓝", "晨曦金"};
		// 集合
		List<String> isp = new ArrayList<String>(); // ISP网络服务提供商
		isp.add("移动");
		isp.add("联通");
		isp.add("电信");
		// Map
		Map<String, String> mainParam = new HashMap<String, String>();
		mainParam.put("runtimeMemory", "8GB");
		mainParam.put("mobileMemory", "128GB");
		mainParam.put("CPUType", "HUAWEI Kirin 990 5G(麒麟990 5G)");
		mainParam.put("CPUCount", "八核");
		// Java Bean
		Sensor sensor = new Sensor();
		sensor.setWeightSensor(true);
		sensor.setFingerSensor(true);
		sensor.setOtherSensor("姿态感应器、色温传感器");

		JSONObject jsonObject = new JSONObject();
		jsonObject.element("name", "HUAWEI P40 Pro")
		.element("support5G", true)
		.element("price", 5988)
		.element("productCode", 2601010188703L)
		.element("screenSize", 6.58D)
		.element("jsonNull", JSONNull.getInstance())
		.element("mobileColor", mobileColor)
		.element("isp", isp)
		.element("mainParam", mainParam)
		.element("sensor", sensor);
	
		Map<String, Class<?>> classMap = new HashMap<>();
		classMap.put("mainParam", Map.class);
		classMap.put("sensor", Sensor.class);
		
		Object obj = JSONObject.toBean(jsonObject, Map.class, classMap);
		System.out.println(obj);
	}
}

2.10. 运行JSONObjectStaticToBeanBeanClassClassMap.java

2.11. 新建JSONObjectStaticToBeanJsonConfig.java(使用JSONObject.toBean(JSONObject jsonObject, JsonConfig jsonConfig)方法)

package com.fj.jo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * JSONObject
 * 1. Object JSONObject.toBean(JSONObject jsonObject, JsonConfig jsonConfig)静态方法, 把JSONObject对象转换为Java对象。
 * 会把第二个参数传入一个JsonConfig, JSONConfig可以设置root Class、Class Map。 
 * 2. 会把JSONObject对象的boolean、int、long、Double、String、JSONNull、JSONArray类型的数据封装root Class里面, root Class推荐使用Map.class。
 * 3. JSONObject对象内部JSON对象数据可以根据键匹配Class Map的key, 获得key相对应的类型, 然后把数据封装到这个类型的对象里。
 */
public class JSONObjectStaticToBeanJsonConfig {
	public static void main(String[] args) {
		// 数组
		String[] mobileColor = {"亮黑色", "冰霜银", "零度白", "深海蓝", "晨曦金"};
		// 集合
		List<String> isp = new ArrayList<String>(); // ISP网络服务提供商
		isp.add("移动");
		isp.add("联通");
		isp.add("电信");
		// Map
		Map<String, String> mainParam = new HashMap<String, String>();
		mainParam.put("runtimeMemory", "8GB");
		mainParam.put("mobileMemory", "128GB");
		mainParam.put("CPUType", "HUAWEI Kirin 990 5G(麒麟990 5G)");
		mainParam.put("CPUCount", "八核");
		// Java Bean
		Sensor sensor = new Sensor();
		sensor.setWeightSensor(true);
		sensor.setFingerSensor(true);
		sensor.setOtherSensor("姿态感应器、色温传感器");

		JSONObject jsonObject = new JSONObject();
		jsonObject.element("name", "HUAWEI P40 Pro")
		.element("support5G", true)
		.element("price", 5988)
		.element("productCode", 2601010188703L)
		.element("screenSize", 6.58D)
		.element("jsonNull", JSONNull.getInstance())
		.element("mobileColor", mobileColor)
		.element("isp", isp)
		.element("mainParam", mainParam)
		.element("sensor", sensor);
	
		Map<String, Class<?>> classMap = new HashMap<>();
		classMap.put("mainParam", Map.class);
		classMap.put("sensor", Sensor.class);
		
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setRootClass(Map.class);
		jsonConfig.setClassMap(classMap);
		
		Object obj = JSONObject.toBean(jsonObject, jsonConfig);
		System.out.println(obj);
	}
}

2.12. 运行JSONObjectStaticToBeanJsonConfig.java

2.13. 新建JSONObjectStaticToBeanRootObjectJsonConfig.java(使用JSONObject.toBean(JSONObject jsonObject, Object root, JsonConfig jsonConfig)方法)

package com.fj.jo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * JSONObject
 * 1. Object JSONObject.toBean(JSONObject jsonObject, Object root, JsonConfig jsonConfig)静态方法, 把JSONObject对象转换为Java对象。
 * 2. 第二个参数传入一个root对象, JSONObject根据root对象的类型, 创建新对象, 然后把数据封装到对象里。
 * 3. 第三个参数JsonConfig这个时候设置的root Class, 没有用了, Class Map依然匹配JSONObject对象内部JSON对象数据。
 */
public class JSONObjectStaticToBeanRootObjectJsonConfig {
	public static void main(String[] args) {
		// 数组
		String[] mobileColor = {"亮黑色", "冰霜银", "零度白", "深海蓝", "晨曦金"};
		// 集合
		List<String> isp = new ArrayList<String>(); // ISP网络服务提供商
		isp.add("移动");
		isp.add("联通");
		isp.add("电信");
		// Map
		Map<String, String> mainParam = new HashMap<String, String>();
		mainParam.put("runtimeMemory", "8GB");
		mainParam.put("mobileMemory", "128GB");
		mainParam.put("CPUType", "HUAWEI Kirin 990 5G(麒麟990 5G)");
		mainParam.put("CPUCount", "八核");
		// Java Bean
		Sensor sensor = new Sensor();
		sensor.setWeightSensor(true);
		sensor.setFingerSensor(true);
		sensor.setOtherSensor("姿态感应器、色温传感器");

		JSONObject jsonObject = new JSONObject();
		jsonObject.element("name", "HUAWEI P40 Pro")
		.element("support5G", true)
		.element("price", 5988)
		.element("productCode", 2601010188703L)
		.element("screenSize", 6.58D)
		.element("jsonNull", JSONNull.getInstance())
		.element("mobileColor", mobileColor)
		.element("isp", isp)
		.element("mainParam", mainParam)
		.element("sensor", sensor);
		
		Map<String, Class<?>> classMap = new HashMap<String, Class<?>>();
		classMap.put("sensor", Sensor.class);
		classMap.put("mainParam", HashMap.class);

		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setClassMap(classMap);
		jsonConfig.setRootClass(Map.class); // 无效的

		Object obj = JSONObject.toBean(jsonObject, new HashMap<String, String>(), jsonConfig);
		System.out.println(obj);
	}
}

2.14. 运行JSONObjectStaticToBeanRootObjectJsonConfig.java

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值