读json文件

来源:互联网 发布:sequoiadb巨杉数据库 编辑:程序博客网 时间:2024/06/11 23:52
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"}]


0 0
原创粉丝点击