jsonファイルを読む

1623 ワード

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONObject;

import com.boventech.zyk.core.entity.Resource;

public class JsonTest {
	public static void main(String[] args) {
		String jsonString = ReadFile("XXX");
		JSONArray myJsonArray = new JSONArray(jsonString);
		for(int i=0 ; i < myJsonArray.length() ;i++){
			JSONObject jsonObject = myJsonArray.getJSONObject(i);
			Resource option = new Resource();
			String name = (String) jsonObject.get("name");
			String description = (String) jsonObject.get("description");
			String optionType = (String) jsonObject.get("type");
			
		}
	}

	public static String ReadFile(String path) {
		File file = new File(path);
		BufferedReader reader = null;
		String laststr = "";
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {
				//System.out.println(tempString.trim());
				laststr = laststr + tempString.trim();
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return laststr;
	}
}

jsonファイル:
[
		{"name":"log","description":"log","type":"IMAGE"},
		{"name":"text-align","description":"text-align","type":"TEXT"}

	]