Java read local json file

background

Before always get a Java reptile, crawling save the information into the database. But this is a course design, the design of front-end GUI, when the display data is the beginning of a direct lookup from the database through a select statement, but I am worried to the teacher, the teacher should JDBC configuration parameters to create the database into a table, etc. Some complicated operation, you want to save locally. Last night saw the students a json file exported from a database, read information from json file, read think this is a good way, so learning a little, tidy here.

Development environment

1 JDK1.8

2 IntelliJ IDEA

3 IDEA comes Maven

json file

{
   "RECORDS" : [ 
    {
       "MovieID": "1" ,
       "name": "The Shawshank Redemption Shawshank Redemption at The" ,
       "Director": "Frank Darabont" ,
       "Scenarist": "Frank de Bonthe / Stephen King " ,
       " Actors ":" Tim Robbins / Morgan Freeman / Bob Okazaki Dayton / William Sadler / Clancy Brown / Gil Bellows / Mark Rolston / James Hui Temo / Jeffrey Friedman / Larry Brandenburg / Neil Jean Toledo / Brian Libby / general David Wahl / Joseph Ragno / Jude Seck Lira / Paul Mike Ranney / Renee Bryan / Alfonso Freeman / V · J · Foster / Frank Medrano / Mark Meyer Adams / Neil Summers / Schneider · Bahrami / Brian Dai Late / Don McManus " ,
       " of the type ":" crime drama " ,
       " ratingNum ":" 9.7 " ,
       " tags ":"Faith in human freedom classic inspirational life story America " 
    }, 
    {
       " MovieID ":" 2 " ,
      "name": "Farewell My Concubine" ,
       "Director": "Chen Kaige" ,
       "Scenarist": "reed / Book Review" ,
       "Actors": "Leslie / Zhang Fengyi / Gong Li / Ge / Indah / Jiang Wenli / David Wu / Lu Qi / Ray Han / Yoon governance / Mingwei Ma / fee Cheung / a Tong Chi / Li / Hailong / Dan / child brother / Chen Huifen / Huang Fei " ,
       " of the type ":" homosexual love story " ,
       " ratingNum ":" 9.6 " ,
       "Tags": "the classic story of human life during the Cultural Revolution, Comrade Romances" 
    }, 
    {
       "MovieID": "3" ,
       "name": "Forrest Gump to Forrest" ,
       "Director": "Robert Zemeckis' ,
       "Scenarist":"Eric Ross / Winston Groom" ,
      "actors": "Tom Hanks / Robin Wright / Gary Sinise / McKay Voltaire Williamson / Sally Field / Haley Joel Osment / Michael Conor Humphries Rees / Harold · G · He Semu / Sam Anderson / ione · M · Te Leiqi / Peter Road Berson / Xi Fang Fallon / Elizabeth Hanks / Hannah Hall / Christopher Jones / Rob Landry / Jason McGuire / Sonny Shi Luoye / Ed Davis / Daniel C. Parker Strip / David Brisbin / Deborah Maike Di Er / Al Harrington / A non-Mo O'Meara / John Ward Amare / Michael Burgess / Eric Underwood / Byron Cummings / Stephen Phuket gwo special / John William Galt / Hillary Shapu Lan / Isabel Ross / Richard DAlessandro / Dick Stilwell / Michael - Jess / Jeffrey Black / Vanessa Ross / Dick Cavett / Mara Suchanek Leite Zha / Joe Allah Braschi / W · Benson Terry " ,
       " of the type ":" love story " ,
       " ratingNum ":" 9.5 " ,
       " tags ":" inspirational classic American life growing belief drama Human " 
    } 
  ] 
}

 

 

Note that this is the json file into resources files

pom-dependent

 <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</version>
 </dependency>

Read JSON Tools

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import java.io.*;


public class JsonTest {

    //读取json文件
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static void main(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);
        }
    }
}

 

 

Guess you like

Origin www.cnblogs.com/wkfvawl/p/11876107.html