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发出如下警告。

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

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

json 例子

json 例子_json-简单的例子相关推荐

  1. 生活中java继承例子_简单继承例子:java

    通用类,来继承出圆和矩形. package circle; public class Geometric { private String color="white"; priva ...

  2. java 内存例子_简单的例子 关于Java内存管理的讲解

    我想做的是,逐行读取文件,然后用该行的电影名去获取电影信息.因为源文件较大,readlines()不能完全读取所有电影名,所以我们逐行读取. 就这段代码,我想要在位置二处使用base64,然后结果呢? ...

  3. matlab m 文件例子,一个简单OFDM例子(matlab m文件)

    经常会遇到有人向我要一个OFDM的程序. 最近翻出了以前写的一个小程序,仿真的是学校时lab的赵玉萍老师1997年VTC上的一篇关于OFDM信道估计的论文的结果. 压缩包中包含了matlab程序和那篇 ...

  4. blockchain 区块链200行代码:在JavaScript实现的一个简单的例子

    blockchain 区块链200行代码:在JavaScript实现的一个简单的例子 了解blockchain的概念很简单(区块链,交易链块):它是分布式的(即不是放置在同一台机器上,不同的网络设备上 ...

  5. pycharm安装scrapy失败_Scrapy ——环境搭配与一个简单的例子

    在我刚接触爬虫的时候就已经听过Scrapy大名了,据说是一个很厉害的爬虫框架,不过那个时候沉迷于Java爬虫.现在终于要来揭开它神秘的面纱了,来一起学习一下吧 欢迎关注公众号:老白和他的爬虫 1.环境 ...

  6. 用几个最简单的例子带你入门 Python 爬虫

    作者 | ZackSock 来源 | 新建文件夹X(ID:ZackSock) 头图 | CSDN下载自视觉中国 前言 爬虫一直是Python的一大应用场景,差不多每门语言都可以写爬虫,但是程序员们却独 ...

  7. 图解爬虫,用几个最简单的例子带你入门Python爬虫

    一.前言 爬虫一直是Python的一大应用场景,差不多每门语言都可以写爬虫,但是程序员们却独爱Python.之所以偏爱Python就是因为她简洁的语法,我们使用Python可以很简单的写出一个爬虫程序 ...

  8. 通过ganache与以太坊Dapp实现交互 —— 简单的例子

    通过ganache与以太坊Dapp实现交互 -- 简单的例子 参考视频来源:链接: 以太坊Dapp开发教程. 准备条件: 环境:Centos7或者其他版本的linux 必备: 安装npm (推荐16. ...

  9. ExecutorService与Executors例子的简单剖析(转)

    对于多线程有了一点了解之后,那么来看看java.lang.concurrent包下面的一些东西.在此之前,我们运行一个线程都是显式调用了 Thread的start()方法.我们用concurrent下 ...

  10. 理解神经网络,从简单的例子开始(2)使用python建立多层神经网络

    这篇文章将讲解如何使用python建立多层神经网络.在阅读这篇文章之前,建议先阅读上一篇文章:理解神经网络,从简单的例子开始.讲解的是单层的神经网络.如果你已经阅读了上一篇文章,你会发现这篇文章的代码 ...

最新文章

  1. 一开工,就遇到上亿(MySQL)大表的优化,我的天...
  2. trigger() --工作中问题nav样式
  3. dubbo服务调试管理实用命令
  4. JQuery eval函数
  5. extern 用法,全局变量与头文件(重复定义)
  6. 新闻发布项目——实体类(newsTb)
  7. 微信公众号页面模版怎么添加文章推荐功能
  8. Hive大数据-认识Hive知识结构_以及概念介绍---大数据之Hive工作笔记0001
  9. desktop viewer
  10. tcp rst 情况
  11. CA数字证书是什么意思?SSL证书与CA数字证书有什么区别?
  12. 新版烽火HG680-LC、CM211-1zg、M304A ZN、MGV2000爱家tv通刷固件(免拆机)
  13. IDEA的配置设置及使用
  14. Crackme 22
  15. 什么是格局、境界、眼界、眼光
  16. [附源码]java毕业设计SSM归途中流浪动物收容与领养管理系统
  17. STK之Commu模块之三仿真卫星通信链路参数计算
  18. android手机怎么拍月亮,用手机拍月亮!对,你没看错
  19. 科技云报道:从“地摊经济”看云计算,产业寡头化趋势进一步加强
  20. 小A的年前面试经历——实录

热门文章

  1. 同步时序逻辑与异步时序逻辑
  2. What is Closure
  3. Vim安装使用和配置
  4. 说一下syslog日志吧~~~
  5. Linux Buffers和Cached的区别(转)
  6. Hibernate之ID生成规则
  7. sqlserver 中怎样查看一个数据库中表的关系
  8. 如何使用数据库引擎优化顾问优化数据库
  9. Replace Record with Data Class
  10. installshield mysql_installshield安装文件的制作小技巧