- 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 - 基本数据类型、Map、列表
- 基本数据类型、对象、Map、列表
- 自定义示例
- JSON.simple - 自定义输出
- 自定义输出流
- JSON.simple 实用资源
- JSON.simple - 快速指南
- JSON.simple - 实用资源
- JSON.simple - 讨论
JSON.simple - 编码 JSONArray
使用 JSON.simple,我们可以通过以下方式对 JSON 数组进行编码 −
编码 JSON 数组 - 输出为字符串 − 简单编码。
编码 JSON 数组 - 输出流 − 输出可用于流。
编码 JSON 数组 - 使用列表 − 使用列表进行编码。
编码 JSON 数组 - 使用列表和输出流 − 使用列表和输入流进行编码。
以下示例演示了以上概念。
示例
import java.io.IOException;
import java.io.StringWriter;
import java.util.LinkedList;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONValue;
class JsonDemo {
public static void main(String[] args) throws IOException {
JSONArray list = new JSONArray();
String jsonText;
list.add("foo");
list.add(new Integer(100));
list.add(new Double(1000.21));
list.add(new Boolean(true));
list.add(null);
jsonText = list.toString();
System.out.println("Encode a JSON Array - to String");
System.out.print(jsonText);
StringWriter out = new StringWriter();
list.writeJSONString(out);
jsonText = out.toString();
System.out.println("\nEncode a JSON Array - Streaming");
System.out.print(jsonText);
List list1 = new LinkedList();
list1.add("foo");
list1.add(new Integer(100));
list1.add(new Double(1000.21));
list1.add(new Boolean(true));
list1.add(null);
jsonText = JSONValue.toJSONString(list1);
System.out.println("\nEncode a JSON Array - Using List");
System.out.print(jsonText);
out = new StringWriter();
JSONValue.writeJSONString(list1, out);
jsonText = out.toString();
System.out.println("\nEncode a JSON Array - Using List and Stream");
System.out.print(jsonText);
}
}
输出
Encode a JSON Array - to String ["foo",100,1000.21,true,null] Encode a JSON Array - Streaming ["foo",100,1000.21,true,null] Encode a JSON Array - Using List ["foo",100,1000.21,true,null] Encode a JSON Array - Using List and Stream ["foo",100,1000.21,true,null]
广告