博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
json 例子_json-简单的例子
阅读量:2539 次
发布时间:2019-05-11

本文共 4914 字,大约阅读时间需要 16 分钟。

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 and 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内部使用和进行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 . 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文件中添加它的依赖项。

com.googlecode.json-simple
json-simple
1.1.1

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") on 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
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 KumarAge = 32City = New YorkCity = BangaloreCity = 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 or . You can also give a try that got added into Java 7.

这就是json-simple的快速总结。 但是,如果要使用复杂的JSON数据,则应使用或 。 您也可以尝试将添加到Java 7中。

翻译自:

json 例子

转载地址:http://cmlzd.baihongyu.com/

你可能感兴趣的文章
【Noip2015pj】求和
查看>>
深入理解JavaScript——闭包
查看>>
C#-WebForm-css box-shadow 给边框添加阴影效果
查看>>
objective-c 编程总结(第七篇)运行时操作 - 动态属性
查看>>
C_数据结构_链表
查看>>
kettle-连接控件
查看>>
Coursera--Neural Networks and Deep Learning Week 2
查看>>
C#中的委托和事件(续)【来自张子扬】
查看>>
机器学习部分国内牛人
查看>>
模拟Sping MVC
查看>>
Luogu 3261 [JLOI2015]城池攻占
查看>>
java修饰符
查看>>
C# Using 用法
查看>>
javascript函数的几种写法集合
查看>>
BZOJ第1页养成计划
查看>>
漫漫修行路
查看>>
js与jQuery的区别——每日一记录
查看>>
MyBatis 处理sql中的 大于,小于,大于等于,小于等于
查看>>
Lunix文件的读写权限问题
查看>>
Liferay 7:portlet name
查看>>