前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Java 处理json经常使用代码

Java 处理json经常使用代码

作者头像
全栈程序员站长
发布2022-07-08 19:42:56
发布2022-07-08 19:42:56
49400
代码可运行
举报
运行总次数:0
代码可运行

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

代码语言:javascript
代码运行次数:0
运行
复制
本project代码已上传至资源,如有须要,请自行下载。
代码语言:javascript
代码运行次数:0
运行
复制
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.cn/115940.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档