Json Simple 简明教程

JSON.simple - Customized Output Streaming

我们可以根据自定义类定制 JSON 流输出。唯一的需求是实现 JSONStreamAware 接口。

以下示例说明了上述概念。

Example

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONStreamAware;
import org.json.simple.JSONValue;

class JsonDemo {
   public static void main(String[] args) throws IOException {
      JSONArray students = new JSONArray();
      students.add(new Student(1,"Robert"));
      students.add(new Student(2,"Julia"));
      StringWriter out = new StringWriter();
      students.writeJSONString(out);
      System.out.println(out.toString());
   }
}
class Student implements JSONStreamAware {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public void writeJSONString(Writer out) throws IOException {
      Map obj = new LinkedHashMap();
      obj.put("name", name);
      obj.put("rollNo", new Integer(rollNo));
      JSONValue.writeJSONString(obj, out);
   }
}

Output

[{name:"Robert",rollNo:1},{name:"Julia",rollNo:2}]