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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 软件包管理(三)–编译安装

    软件包管理(三)–编译安装

    2021年6月10日
    108
  • 论文写作——origin画图[通俗易懂]

    论文写作——origin画图[通俗易懂]一origin的安装详见下面网址,内涵下载路径和破解方法。http://www.ddooo.com/softdown/51005.htm二origin画图1、柱状图①打开后页面如下所示。A(X)代表X轴的数据,B(Y)代表Y轴的数据。②将数据填入中间的book1中。book的作用和Excel中很类似,我们可以按照自己的需要添加sheet,添加book。我们将…

    2022年4月19日
    74
  • 小程序 table_小程序下滑样式

    小程序 table_小程序下滑样式先看效果图wxml<viewclass=”mdui-m-t-2mudi-f-s-24″>1.养老保险</view><viewclass=”mdui-m-t-1mudi-indentmudi-f-s-20″>养老保险,全称社会基本养老保险,是国家和社会根据一定的法律和法规,为解决劳动者在达到国家规定的解除劳动义务的劳动…

    2022年9月15日
    0
  • docker卸载命令_删除docker

    docker卸载命令_删除docker删除容器(jenkins官网提供的安装方式,删除比较特殊,因为jenkins自己创建了数据卷,所以要删除数据卷)不然,就算删除了容器,再运行镜像,以前的配置还是删不掉,运行的还是以前的配置首先,关停并删除jenkins容器dockerstop容器iddockerrm容器id然后,查看数据卷(如果是用挂载目录方式安装的jenkins的话,就不需要执行下面的步骤了)dockervolumels发现一个jenkins_home的数据卷,删除数据卷dockervolume

    2022年9月7日
    0
  • HTML中空格_如何去掉word中的空格

    HTML中空格_如何去掉word中的空格1. 空格符大  家  好!显示效果为:大家好!2.p标签使用空两格百度地图API功能百度地图API功能效果如下:3.span标签空格百度地图API功能百度地图API功能效果如下:4.word-spacing属性,Happynewyear!效果:![在这里插入图片描述](https://img-blog….

    2022年9月28日
    0
  • 方法区(Method Area)存储的静态变量[通俗易懂]

    方法区(Method Area)存储的静态变量[通俗易懂]1:方法区(MethodArea)存储的静态变量静态变量又称为类变量,类中被static修饰的成员变量都是静态变量(类变量)静态变量之所以又称为类变量,是因为静态变量和类关联在一起,随着类的加载而存在于方法区(而不是堆中)八种基本数据类型(byte、short、int、long、float、double、char、boolean)的静态变量会在方法区开辟空间,并将对应的值存储在方法方…

    2022年5月18日
    45

发表回复

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

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