JSON.simple example – Read and write JSON
来源:互联网 发布:qq飞车青峰剃刀数据 编辑:程序博客网 时间:2024/05/17 23:00
JSON.simple example – Read and write JSON
JSON.simple, is a simple Java library for JSON processing, read and write JSON data and full compliance withJSON specification (RFC4627).
Note
To convert object to / from JSON, you should consider Jackson or Gson.
To convert object to / from JSON, you should consider Jackson or Gson.
In this tutorial, we show you how to use JSON.simple to read and write JSON data from / to a file.
1. JSON.simple Dependency
JSON.simple is available at Maven central repository, just declares following dependency in yourpom.xml
file.
<dependency><groupId>com.googlecode.json-simple</groupId><artifactId>json-simple</artifactId><version>1.1</version> </dependency>
2. Write JSON to file
In below example, it write JSON data via JSONObject
and JSONArray
, and save it into a file named “test.json“.
import java.io.FileWriter;import java.io.IOException;import org.json.simple.JSONArray;import org.json.simple.JSONObject; public class JsonSimpleExample { public static void main(String[] args) { JSONObject obj = new JSONObject();obj.put("name", "mkyong.com");obj.put("age", new Integer(100)); JSONArray list = new JSONArray();list.add("msg 1");list.add("msg 2");list.add("msg 3"); obj.put("messages", list); try { FileWriter file = new FileWriter("c:\\test.json");file.write(obj.toJSONString());file.flush();file.close(); } catch (IOException e) {e.printStackTrace();} System.out.print(obj); } }
Output – See content of file named “test.json“.
{"age":100,"name":"mkyong.com","messages":["msg 1","msg 2","msg 3"]}
3. Read JSON from file
Use JSONParser
to read above generated JSON file “test.json“, and display each of the values.
import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;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 JsonSimpleExample { public static void main(String[] args) { JSONParser parser = new JSONParser(); try { Object obj = parser.parse(new FileReader("c:\\test.json")); JSONObject jsonObject = (JSONObject) obj; String name = (String) jsonObject.get("name");System.out.println(name); long age = (Long) jsonObject.get("age");System.out.println(age); // loop arrayJSONArray msg = (JSONArray) jsonObject.get("messages");Iterator<String> iterator = msg.iterator();while (iterator.hasNext()) {System.out.println(iterator.next());} } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();} } }
Output
mkyong.com100msg 1msg 2msg 3
References
- JSON.simple official website
- JSON.simple encoding JSON example
- JSON.simple decoding JSON example
- JSON.simple example – Read and write JSON Read
- JSON.simple example – Read and write JSON
- Android read write json
- Jackson Streaming API To Read And Write JSON
- How to read and write JSON files in Java(Gson)
- UIPasteboard Example – Read, Write and Share data between apps
- JSON and Servlet example
- Example : Linux kernal file read and write
- properties read write example
- QueryBuilder: A Simple Way to Read and Write Query Strings
- json example
- JSON example
- Spring 3 MVC and JSON example
- write json
- json-simple
- read json
- Read and Write XML
- Read and Write Excel
- Examples of JSON encoding
- Ubuntu更新命令
- JQuery mouse..事件+元素显示隐藏 切换 +淡入淡出+滑动效果。(可以做一个ul li下拉框滑动收起效果:slideUp)
- unity参数重载调用
- exit _exit _Exit
- JSON.simple example – Read and write JSON
- Linux远程联机服务器SSH VNC XDMCP RDP
- ActionBar与Fragment
- webservice编写和使用
- SUSE10安装memcached
- iOS(1): iOS7 隐藏Status Bar
- java帮助文档 JDK帮助文档(chm版) (所有版本提供下载)
- Java中Array与ArrayList的主要区别
- Java JSON Tutorial