importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONArray;importcom.alibaba.fastjson.JSONObject;import java.io.*;public classJsonTest {//读取json文件
public staticString readJsonFile(String fileName) {
String jsonStr= "";try{
File jsonFile= newFile(fileName);
FileReader fileReader= newFileReader(jsonFile);
Reader reader= new InputStreamReader(new FileInputStream(jsonFile),"utf-8");int ch = 0;
StringBuffer sb= newStringBuffer();while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr=sb.toString();returnjsonStr;
}catch(IOException e) {
e.printStackTrace();return null;
}
}public static voidmain(String[] args) {
String path= JsonTest.class.getClassLoader().getResource("Movie.json").getPath();
String s=readJsonFile(path);
JSONObject jobj=JSON.parseObject(s);
JSONArray movies= jobj.getJSONArray("RECORDS");//构建JSONArray数组
for (int i = 0 ; i < movies.size();i++){
JSONObject key=(JSONObject)movies.get(i);
String name= (String)key.get("name");
String director= (String)key.get("director");
String scenarist=((String)key.get("scenarist"));
String actors=((String)key.get("actors"));
String type=((String)key.get("type"));
String ratingNum=((String)key.get("ratingNum"));
String tags=((String)key.get("tags"));
System.out.println(name);
System.out.println(director);
System.out.println(scenarist);
System.out.println(actors);
System.out.println(type);
System.out.println(director);
System.out.println(ratingNum);
System.out.println(tags);
}
}
}