JSON字符串转换为对象

首先理解JSONObject JSONArray,JSONObject就代表{“key”:"value","key":"value"}格式,不管多么复杂都是这种格式,JSONArray代表[{"key":"value"},{"key":"value"}]格式,原理一样,不管多么复杂,都是这种格式。

 

<dependency>

<groupId>net.sf.json-lib</groupId>

<artifactId>json-lib</artifactId>

<version>2.4</version>

<classifier>jdk15</classifier>

</dependency>

 

下面例子展示了怎么把对象转换成字符串,把复杂字符串解析成对象

package JSON.JSONObjectAndJSONArray;

import org.junit.Before;

import JSON.entity.Student;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Unit test for simple App.
 */
public class AppTest extends TestCase {
	/**
	 * Create the test case
	 *
	 * @param testName
	 *            name of the test case
	 */
	public AppTest(String testName) {
		super(testName);
	}

	/**
	 * @return the suite of tests being tested
	 */
	public static Test suite() {
		return new TestSuite(AppTest.class);
	}

	/**
	 * Rigourous Test :-)
	 */
	@org.junit.Test
	public void testApp() {
		Student stu1 = new Student("001", "小红", "24");
		Student stu2 = new Student("002", "小黄", "25");
		Student stu3 = new Student("003", "小绿", "23");
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();

		jsonObject.put("stu1", stu1);
		jsonObject.put("stu2", stu2);
		jsonObject.put("stu3", stu3);

		String jsonString = jsonObject.toString();
		System.out.println(jsonString);
		// {"stu1":{"age":"24","id":"001","name":"小红"},"stu2":{"age":"25","id":"002","name":"小黄"},"stu3":{"age":"23","id":"003","name":"小绿"}}

		jsonArray.add(stu1);
		jsonArray.add(stu2);
		jsonArray.add(stu3);
		System.out.println(jsonArray.toString());
		// [{"age":"24","id":"001","name":"小红"},{"age":"25","id":"002","name":"小黄"},{"age":"23","id":"003","name":"小绿"}]

		jsonObject = new JSONObject();
		jsonObject.put("student", jsonArray);
		System.out.println(jsonObject);
		// {"student":[{"age":"24","id":"001","name":"小红"},{"age":"25","id":"002","name":"小黄"},{"age":"23","id":"003","name":"小绿"}]}

		System.out.println("----------------");

		String str = "{\"stu1\":{\"age\":\"24\",\"id\":\"001\",\"name\":\"小红\"},\"stu2\":{\"age\":\"25\",\"id\":\"002\",\"name\":\"小黄\"},\"stu3\":{\"age\":\"23\",\"id\":\"003\",\"name\":\"小绿\"}}";
		JSONObject jsonObject2 = JSONObject.fromObject(str);
		Object object = jsonObject2.get("stu1");
		System.out.println(object);
		// {"age":"24","id":"001","name":"小红"}

		JSONObject jsonBean = JSONObject.fromObject(object);
		System.out.println("jsonBean---" + jsonBean);
		// jsonBean---{"age":"24","id":"001","name":"小红"}

		Object bean = JSONObject.toBean(jsonBean, Student.class);
		if (bean instanceof Student) {
			System.out.println(bean);
			// Student [id=001, name=小红, age=24]
		}
		System.out.println("--------------");

		String str2 = "{\"student\":[{\"age\":\"24\",\"id\":\"001\",\"name\":\"小红\"},{\"age\":\"25\",\"id\":\"002\",\"name\":\"小黄\"},{\"age\":\"23\",\"id\":\"003\",\"name\":\"小绿\"}]}";
		JSONObject jsonObject3 = JSONObject.fromObject(str2);
		Object object2 = jsonObject.get("student");
		JSONArray jsonArray2 = JSONArray.fromObject(object2);
		for (int i = 0; i < jsonArray2.size(); i++) {
			Object object3 = jsonArray2.get(i);
			JSONObject object4 = jsonObject.fromObject(object3);
			Object bean2 = JSONObject.toBean(object4, Student.class);
			if (bean2 instanceof Student) {
				System.out.println(bean2);
				/*
				 * Student [id=001, name=小红, age=24] 
				 * Student [id=002, name=小黄, age=25] 
				 * Student [id=003, name=小绿, age=23]
				 */
			}
		}

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值