json 例子
json-simple is a simple java toolkit for JSON. json-simple library is fully compliance with JSON specification (RFC4627).
json-simple是用于JSON的简单Java工具包。 json-simple库完全符合JSON规范(RFC4627)。
json-简单 (json-simple)
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-simple内部使用Map和List进行JSON处理。 我们可以使用json-simple来解析JSON数据以及将JSON写入文件。 json-simple的最佳功能之一是它不依赖于任何第三方库。 json-simple是非常轻量级的API,可以很好地满足简单的JSON要求。
json-简单的Maven (json-simple maven)
We can add json-simple library to our project by downloading it from here. Since json-simple is available in maven central repository, best way is to add it’s dependency in pom.xml file.
我们可以通过从此处下载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-简单的示例,将JSON写入文件 (json-simple example to write JSON to file)
Most important class in json-simple API is org.json.simple.JSONObject
. We create instance of JSONObject
and put key-value pairs into it. JSONObject toJSONString
method returns the JSON in String format that we can write to file.
json-simple API中最重要的类是org.json.simple.JSONObject
。 我们创建JSONObject
实例,并将键值对放入其中。 JSONObject toJSONString
方法以可写入文件的String格式返回JSON。
For writing list to a JSON key, we can use org.json.simple.JSONArray
.
为了将列表写入JSON密钥,我们可以使用org.json.simple.JSONArray
。
- package com.journaldev.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());
-
- }
-
- }
Above class will write data.json
, below is the JSON content of this file.
上面的类将写入data.json
,下面是此文件的JSON内容。
{"cities":["New York","Bangalore","San Francisco"],"name":"Pankaj Kumar","age":32}
Notice the @SuppressWarnings("unchecked")
annotation on main method? This was done to avoid warnings related to Type safety. JSONObject
extends HashMap but doesn’t support Generics, so Eclipse IDE gives warning as below.
注意主方法上的@SuppressWarnings("unchecked")
注释吗? 这样做是为了避免与类型安全有关的警告。 JSONObject
扩展了HashMap,但不支持泛型,因此Eclipse IDE发出如下警告。
json-简单的示例,从文件中读取JSON (json-simple example to read JSON from file)
For reading JSON from file, we have to use org.json.simple.parser.JSONParser
class. JSONParser parse
method returns JSONObject. Then we can retrieve values by passing key names. Below is json-simple example to read JSON from file.
为了从文件读取JSON,我们必须使用org.json.simple.parser.JSONParser
类。 JSONParser的parse
方法返回JSONObject。 然后,我们可以通过传递键名称来检索值。 以下是json-simple示例,可从文件读取JSON。
- package com.journaldev.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();
- }
-
- }
Above json-simple example produces following output.
上面的json-simple示例产生以下输出。
- Name = Pankaj Kumar
- Age = 32
- City = New York
- City = Bangalore
- City = San Francisco
That’s all for a quick roundup of json-simple. However if you want to work with complex JSON data, you should use Jackson or Gson. You can also give JSR353 a try that got added into Java 7.
这就是json-simple的快速总结。 但是,如果要使用复杂的JSON数据,则应使用Jackson或Gson 。 您也可以尝试将JSR353添加到Java 7中。
json 例子