欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSON for Java 源代码

程序员文章站 2024-01-30 20:17:40
...
将源代码发布一下与大家一起探讨,不足之处请大家不吝赐教。

1.测试文件:

test.json

/**
* 测试JSON
*
*/
{
id:-NaN, //ID
$name_1:"johnson\"李\"+\"test\"", //姓名
married:false,
hobby:[
"programming"
]
}


2.测试代码

package org.zergle.json;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.zergle.json.utils.JsonOutputFormat;

public class TestJsonReader {

/**
* 测试从文件解析JSON
* @param args
*/
public static void main(String[] args) {
JsonReader reader = null;
JsonWriter writer = null;
try {
reader = new JsonReader(new InputStreamReader(TestJsonReader.class.getResourceAsStream("/test.json")));
JsonObject json = reader.readJsonObject();
writer = new JsonWriter(new OutputStreamWriter(System.out));
writer.write(json, new JsonOutputFormat());
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
reader = null;
}
if (writer != null) {
writer.close();
writer = null;
}
} catch (IOException e) { }
}

}

}