简单的JSON示例

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

简单的Json

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

json-simple maven的意思是使用Maven来管理json-simple库。

我们可以通过从这里下载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的实例并将键值对放入其中。JSONObject的toJSONString方法将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类。JSONParser解析方法返回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。

发表回复 0

Your email address will not be published. Required fields are marked *


广告
将在 10 秒后关闭
bannerAds