json-simple简明教程

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 with JSON specification (RFC4627).

Note
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 your pom.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 array
                  JSONArray 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.com
100
msg 1
msg 2
msg 3

References
JSON.simple official website
JSON.simple encoding JSON example
JSON.simple decoding JSON example


原文链接:
http://www.mkyong.com/java/json-simple-example-read-and-write-json/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北雨南萍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值