Java 处理json经常使用代码

Java 处理json经常使用代码

大家好,又见面了,我是全栈君。

本project代码已上传至资源,如有须要,请自行下载。
package com.michael;

import static org.junit.Assert.assertEquals;

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

import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Test;

public class JsonLibTest {
	/**
	 * 将数组转换为json对象
	 */
	// @Test
	public void testArrayToJSON() {
		boolean[] boolArray = new boolean[] { true, false, true };
		JSONArray jsonArray = JSONArray.fromObject(boolArray);
		System.out.println("testArrayToJSON----jsonArray-----" + jsonArray);
	}

	/**
	 * 将集合转换为json
	 */
	// @Test
	public void testListToJSON() {
		List<String> list = new ArrayList<String>();
		list.add("first");
		list.add("second");
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println("testListToJSON---jsonArray----" + jsonArray);
	}

	/**
	 * 将json字符串转换为json对象
	 */
	@Test
	public void testJsonStrToJSON() {
		JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");
		System.out.println("testJsonStrToJSON---jsonArray----" + jsonArray);
	}

	/**
	 * 将map转换为json对象
	 */
	@Test
	public void testMapToJSON() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "json");
		map.put("bool", Boolean.TRUE);
		map.put("int", new Integer(1));
		map.put("arr", new String[] { "a", "b" });
		map.put("func", "function(i){ return this.arr[i]; }");
		JSONObject jsonObject = JSONObject.fromObject(map);
		System.out.println("testJsonStrToJSON---jsonObject----" + jsonObject);
	}

	// 将实体类转换为json
	@Test
	public void testBeadToJSON() {
		Car bean = new Car();
		bean.setId("001");
		bean.setName("hello");
		bean.setDate(new Date());

		List<Car> cars = new ArrayList<Car>();
		cars.add(new Car("AUDI"));
		cars.add(new Car("BMW"));
		cars.add(new Car("QQ"));
		// cars.add(new Person("test"));
		bean.setCars(cars);
		JSONObject jsonObject = JSONObject.fromObject(bean);
		System.out.println("testBeadToJSON---jsonObject----" + jsonObject);

	}

	@Test
	/**
	 * 将json字符串转换为对象
	 * @throws Exception
	 */
	public void testJSONToObject() throws Exception {
		String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
		JSONObject jsonObject = JSONObject.fromObject(json);
		Object bean = JSONObject.toBean(jsonObject);
		assertEquals(jsonObject.get("name"),
				PropertyUtils.getProperty(bean, "name"));
		assertEquals(jsonObject.get("bool"),
				PropertyUtils.getProperty(bean, "bool"));
		assertEquals(jsonObject.get("int"),
				PropertyUtils.getProperty(bean, "int"));
		assertEquals(jsonObject.get("double"),
				PropertyUtils.getProperty(bean, "double"));
		assertEquals(jsonObject.get("func"),
				PropertyUtils.getProperty(bean, "func"));

		List arrayList = (List) JSONArray.toCollection(jsonObject
				.getJSONArray("array"));
		for (Object object : arrayList) {
			System.out.println(object);
		}

	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115940.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • c++截取字符串[通俗易懂]

    c++截取字符串[通俗易懂]C++的string类提供了大量的字符串操作函数,提取字符串的一部分,可采用substr函数实现

    2022年5月10日
    63
  • 查看进程的命令 linux_centos查看运行的进程

    查看进程的命令 linux_centos查看运行的进程关键字:linux查进程、杀进程、起进程1.查进程   ps命令查找与进程相关的PID号:   psa显示现行终端机下的所有程序,包括其他用户的程序。   ps-A显示所有程序。   psc列出程序时,显示每个程序真正的指令名称,而不包含路径,参数或常驻服务的标示。   ps-e此参数的效果和指定”A”参数相同。   pse列出程序时

    2022年9月21日
    2
  • linux用netstat查看服务及监听端口

    linux用netstat查看服务及监听端口

    2021年10月27日
    60
  • Redis分布式锁的正确实现方式(Java版)

    Redis分布式锁的正确实现方式(Java版)https://wudashan.cn/2017/10/23/Redis-Distributed-Lock-Implement/https://blog.csdn.net/l_bestcoder/article/details/79336986一、什么是分布式锁?要介绍分布式锁,首先要提到与分布式锁相对应的是线程锁、进程锁。线程锁:主要用来给方法、代码块加锁。当某个方法或代码使用锁,…

    2022年6月4日
    39
  • python count()函数

    python count()函数Python元组count()方法用于统计某个元素在元祖,列表,字符串中出现的次数。可选参数为在字符串搜索的开始与结束位置。参数sub–搜索的子字符串start–字符串开始搜索

    2022年7月5日
    20
  • executorservice 线程池_并发数与线程数

    executorservice 线程池_并发数与线程数keepAliveTime:表示线程没有任务执行时最多保持多久时间会终止。默认情况下,只有当线程池中的线程数大于corePoolSize时,keepAliveTime才会起作用,直到线程池中的线程数不大于corePoolSize,即当线程池中的线程数大于corePoolSize时,如果一个线程空闲的时间达到keepAliveTime,则会终止,直到线程池中的线程数不超过corePoolSize。但…

    2025年10月18日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号