---
title: "JSON for Java 源代码"
author: "johnson.lee"
date: "2010-02-03T13:58:09Z"
tags: []
categories: ["编程语言"]
slug: "json-for-java"
draft: false
lastmod: "2010-03-03T10:51:55Z"
id: 586686
url: "https://johnson-lee.iteye.com/blog/586686"
---

将源代码发布一下与大家一起探讨，不足之处请大家不吝赐教。

1.测试文件：

test.json

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

```


2.测试代码

```java

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) { }
		}
		
	}

}

```