JSON入门:从简单示例掌握数据交换核心

json-simple 是一个用于处理 JSON 的轻量级 Java 工具包。json-simple 库完全符合 JSON 规范(RFC4627)。

简单的JSON处理:json-simple入门

json-simple 在 JSON 处理中内部使用 Map 和 List。我们可以使用 json-simple 来解析 JSON 数据以及将 JSON 写入文件。json-simple 最好的特点之一是它不依赖于任何第三方库。json-simple 是一个非常轻量级的 API,可以满足简单的 JSON 需求。

json-simple Maven 依赖配置

我们可以通过下载 json-simple 库并将其添加到项目中。由于 json-simple 库在 Maven 中央仓库中可用,最好的方法是将它的依赖添加到 pom.xml 文件中:

<dependency>
	<groupId>com.googlecode.json-simple</groupId>
	<artifactId>json-simple</artifactId>
	<version>1.1.1</version>
</dependency>

使用 json-simple 将 JSON 写入文件示例

在 json-simple API 中,最重要的类是 org.json.simple.JSONObject。我们创建 JSONObject 的实例并将键值对放入其中。JSONObjecttoJSONString 方法将 JSON 以字符串格式返回,我们可以将其写入文件。要将列表写入 JSON 键中,我们可以使用 org.json.simple.JSONArray

package com.Olivia.json.write;

import java.io.FileWriter;
import java.io.IOException;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class JsonSimpleWriter {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		JSONObject obj = new JSONObject();
		
		obj.put("name", "Pankaj Kumar");
		obj.put("age", new Integer(32));

		JSONArray cities = new JSONArray();
		cities.add("New York");
		cities.add("Bangalore");
		cities.add("San Francisco");

		obj.put("cities", cities);

		try {

			FileWriter file = new FileWriter("data.json");
			file.write(obj.toJSONString());
			file.flush();
			file.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.print(obj.toJSONString());

	}

}

以上代码将写入 data.json 文件,其 JSON 内容如下:

{"cities":["New York","Bangalore","San Francisco"],"name":"Pankaj Kumar","age":32}

注意 main 方法上的 @SuppressWarnings("unchecked") 注解吗?这是为了避免与类型安全相关的警告。JSONObject 扩展了 HashMap 但不支持泛型,因此 Eclipse IDE 会给出以下警告:

类型安全性:put(Object, Object)方法属于原始类型HashMap。对泛型类型HashMap<K,V>的引用应该进行参数化。

使用 json-simple 从文件中读取 JSON 示例

读取 JSON 文件时,我们需要使用 org.json.simple.parser.JSONParser 类。JSONParserparse 方法返回 JSONObject 对象。然后,我们可以通过传递键名来检索值。以下是一个使用 json-simple 读取 JSON 文件的示例:

package com.Olivia.json.write;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonSimpleReader {

	public static void main(String[] args) throws ParseException, FileNotFoundException, IOException {
		JSONParser parser = new JSONParser();
		Reader reader = new FileReader("data.json");

		Object jsonObj = parser.parse(reader);

		JSONObject jsonObject = (JSONObject) jsonObj;

		String name = (String) jsonObject.get("name");
		System.out.println("Name = " + name);

		long age = (Long) jsonObject.get("age");
		System.out.println("Age = " + age);

		JSONArray cities = (JSONArray) jsonObject.get("cities");
		
		@SuppressWarnings("unchecked")
		Iterator<String> it = cities.iterator();
		while (it.hasNext()) {
			System.out.println("City = " + it.next());
		}
		reader.close();
	}

}

上面的 json-simple 示例产生了以下输出:

Name = Pankaj Kumar
Age = 32
City = New York
City = Bangalore
City = San Francisco

这是对 json-simple 的快速概述。然而,如果您想处理复杂的 JSON 数据,您应该考虑使用 Jackson 或 Gson。您还可以尝试使用添加到 Java 7 中的 JSR353。

bannerAds