日期:2014-05-16  浏览次数:20349 次

json-lib解析JSON例子

首先说明一下,json-lib相较于Jackson以及GSON,性能是最低的,并且长期没有更新,所以不推荐使用。

1.Maven Dependency

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>org.fool.json</groupId>
	<artifactId>json</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>JSON</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.1</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

先来看一个简单的DEMO

JsonlibDemo.java

package org.fool.json.jsonlib;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonlibDemo
{
	public static void main(String[] args)
	{
		String jsonContent = "{'hello':'world', 'hero':'heroine'}";

		JSONObject jsonObject = JSONObject.fromObject(jsonContent);

		String str1 = jsonObject.getString("hello");
		String str2 = jsonObject.getString("hero");

		System.out.println(str1);
		System.out.println(str2);

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

		jsonContent = "[{'hello': 333, 'abc': false, 'xyz': {'a': 1, 'b': 'ab'}}, "
				+ "{'hello': 555, 'abc': true, 'xyz': {'a': 3, 'b': 'ba'}}]";

		JSONArray jsonArray = JSONArray.fromObject(jsonContent);

		for (Object object : jsonArray)
		{
			JSONObject jsonObject2 = JSONObject.fromObject(object);

			int value1 = jsonObject2.getInt("hello");
			boolean value2 = jsonObject2.getBoolean("abc");
			// String value33 = jsonObject2.getString("xyz");

			JSONObject jsonObject3 = jsonObject2.getJSONObject("xyz");

			int value3 = jsonObject3.getInt("a");
			String value4 = jsonObject3.getString("b");

			System.out.println(value1);
			System.out.println(value2);
			System.out.println(value3);
			System.out.println(value4);
		}
	}
}

?

2.POJO

Person.java

package org.fool.json.jsonlib;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class Person
{
	private String username;

	private String password;

	private int age;

	private String address;

	private List<String> list = new ArrayList<>();

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public int getAge()
	{
		return age;
	}

	public void setAge(int age)
	{
		this.age = age;
	}

	public String getAddress()
	{
		return address;
	}

	public void setAddress(String address)
	{
		this.address = address;
	}

	public List<String> getList()
	{
		return list;
	}

	public void setList(List<String> list)
	{
		this.list = list;
	}

	@Override
	public String toString()
	{
		return ToStringBuilder.reflectionToStr