json 例子_json-简单的例子

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, 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-simple内部使用MapList进行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发出如下警告。

Type safety: The method put(Object, Object) belongs to the raw type HashMap. References to generic type HashMap<K,V> should be parameterized
类型安全:方法put(Object,Object)属于原始类型HashMap。 泛型类型HashMap <K,V>的引用应参数化

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数据,则应使用JacksonGson 。 您也可以尝试将JSR353添加到Java 7中。

翻译自: https://www.journaldev.com/12668/json-simple-example

json 例子

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值