- JSON.simple 教程
- JSON.simple - 主頁
- JSON.simple - 概述
- JSON.simple - 環境設定
- JSON.simple - JAVA 對映
- 解碼示例
- 轉義特殊字元
- JSON.simple - 使用 JSONValue
- JSON.simple - 異常處理
- JSON.simple - 容器工廠
- JSON.simple - 內容處理程式
- 編碼示例
- JSON.simple - 編碼 `JSONObject`
- JSON.simple - 編碼 `JSONArray`
- 合併示例
- JSON.simple - 合併物件
- JSON.simple - 合併陣列
- 組合示例
- JSON.simple - 原始型別、物件、陣列
- JSON.simple - 原始型別、對映、列表
- 原始型別、物件、對映、列表
- 自定義示例
- JSON.simple - 自定義輸出
- 自定義輸出流
- JSON.simple 有用資源
- JSON.simple - 快速指南
- JSON.simple - 有用資源
- JSON.simple - 討論
JSON.simple - ContentHandler
ContentHandler 介面用於提供類似 SAX 的介面來流傳輸大型 JSON。它還提供可停止功能。以下示例說明了此概念。
示例
import java.io.IOException;
import java.util.List;
import java.util.Stack;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ContentHandler;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
class JsonDemo {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
String text = "{\"first\": 123, \"second\": [4, 5, 6], \"third\": 789}";
try {
CustomContentHandler handler = new CustomContentHandler();
parser.parse(text, handler,true);
} catch(ParseException pe) {
}
}
}
class CustomContentHandler implements ContentHandler {
@Override
public boolean endArray() throws ParseException, IOException {
System.out.println("inside endArray");
return true;
}
@Override
public void endJSON() throws ParseException, IOException {
System.out.println("inside endJSON");
}
@Override
public boolean endObject() throws ParseException, IOException {
System.out.println("inside endObject");
return true;
}
@Override
public boolean endObjectEntry() throws ParseException, IOException {
System.out.println("inside endObjectEntry");
return true;
}
public boolean primitive(Object value) throws ParseException, IOException {
System.out.println("inside primitive: " + value);
return true;
}
@Override
public boolean startArray() throws ParseException, IOException {
System.out.println("inside startArray");
return true;
}
@Override
public void startJSON() throws ParseException, IOException {
System.out.println("inside startJSON");
}
@Override
public boolean startObject() throws ParseException, IOException {
System.out.println("inside startObject");
return true;
}
@Override
public boolean startObjectEntry(String key) throws ParseException, IOException {
System.out.println("inside startObjectEntry: " + key);
return true;
}
}
輸出
inside startJSON inside startObject inside startObjectEntry: first inside primitive: 123 inside endObjectEntry inside startObjectEntry: second inside startArray inside primitive: 4 inside primitive: 5 inside primitive: 6 inside endArray inside endObjectEntry inside startObjectEntry: third inside primitive: 789 inside endObjectEntry inside endObject inside endJSON
廣告