json-simple 示例

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

JSON 简单

json-simple, json simple example, json example json-simple uses Map and List internally for JSON processing. We can use json-simple for parsing JSON data as well as writing JSON to file. One of the best feature of json-simple is that it has no dependency on any third party libraries. json-simple is very lightweight API and serves well with simple JSON requirements.

JSON - 简单的 Maven

我们可以将 json-simple 库添加到我们的项目中,通过从 这里下载。因为 json-simple 可在 maven 中央存储中使用,最好的方法是在 pom.xml 文件中添加它的依赖性。

1<dependency>
2    <groupId>com.googlecode.json-simple</groupId>
3    <artifactId>json-simple</artifactId>
4    <version>1.1.1</version>
5</dependency>

json - 简单的例子来写 JSON 到文件

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

 1package com.journaldev.json.write;
 2
 3import java.io.FileWriter;
 4import java.io.IOException;
 5
 6import org.json.simple.JSONArray;
 7import org.json.simple.JSONObject;
 8
 9public class JsonSimpleWriter {
10
11    @SuppressWarnings("unchecked")
12    public static void main(String[] args) {
13    	JSONObject obj = new JSONObject();
14    	
15    	obj.put("name", "Pankaj Kumar");
16    	obj.put("age", new Integer(32));
17
18    	JSONArray cities = new JSONArray();
19    	cities.add("New York");
20    	cities.add("Bangalore");
21    	cities.add("San Francisco");
22
23    	obj.put("cities", cities);
24
25    	try {
26
27    		FileWriter file = new FileWriter("data.json");
28    		file.write(obj.toJSONString());
29    		file.flush();
30    		file.close();
31
32    	} catch (IOException e) {
33    		e.printStackTrace();
34    	}
35
36    	System.out.print(obj.toJSONString());
37
38    }
39
40}

类上方会写data.json,下面是这个文件的JSON内容。

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

注意@SuppressWarnings(未检查) 注释main方法上吗?这是为了避免与类型安全相关的警告。

类型安全: 方法 put(对象,对象)属于原始类型HashMap. 参考通用类型HashMap<K,V>应参数化

json - 简单的例子来从文件中读取 JSON

要从文件中读取 JSON,我们必须使用 org.json.simple.parser.JSONParser 类. JSONParser parse 方法返回 JSONObject. 然后我们可以通过键名传输值。

 1package com.journaldev.json.write;
 2
 3import java.io.FileNotFoundException;
 4import java.io.FileReader;
 5import java.io.IOException;
 6import java.io.Reader;
 7import java.util.Iterator;
 8
 9import org.json.simple.JSONArray;
10import org.json.simple.JSONObject;
11import org.json.simple.parser.JSONParser;
12import org.json.simple.parser.ParseException;
13
14public class JsonSimpleReader {
15
16    public static void main(String[] args) throws ParseException, FileNotFoundException, IOException {
17    	JSONParser parser = new JSONParser();
18    	Reader reader = new FileReader("data.json");
19
20    	Object jsonObj = parser.parse(reader);
21
22    	JSONObject jsonObject = (JSONObject) jsonObj;
23
24    	String name = (String) jsonObject.get("name");
25    	System.out.println("Name = " + name);
26
27    	long age = (Long) jsonObject.get("age");
28    	System.out.println("Age = " + age);
29
30    	JSONArray cities = (JSONArray) jsonObject.get("cities");
31    	
32    	@SuppressWarnings("unchecked")
33    	Iterator<String> it = cities.iterator();
34    	while (it.hasNext()) {
35    		System.out.println("City = " + it.next());
36    	}
37    	reader.close();
38    }
39
40}

上面的 json 简单示例产生以下输出。

1Name = Pankaj Kumar
2Age = 32
3City = New York
4City = Bangalore
5City = San Francisco

但是,如果你想使用复杂的 JSON 数据,你应该使用 JacksonGson

Published At
Categories with 技术
Tagged with
comments powered by Disqus