2016-09-12 5 views
0
でJSONファイル

を使用してJSONファイルとして私は、JSONファイルをインポートして、その中のデータを使用してファイルをエクスポートしようとしているJavaの

ムービーライブラリを初期化し、に変更JSONファイルをエクスポートするファイルmovies.jsonを使用しますプログラムの完了時にライブラリを保存します。

private Vector<MovieDescription> libraryList = new Vector<MovieDescription>(); 

public static void main(String args[]) { 

    try { 

     MovieLibrary movies = new MovieLibrary(); 

     // Input the group from movies.json 

     System.out.println("Importing group from movies.json"); 

     MovieLibrary movies1 = new MovieLibrary("movies.json"); 

     movies1.add(new MovieDescription("Suicide Squad", "PG-13", "05 August 2016", " 2h 3min", 
       "Fearing that the world is vulnerable to otherworldly threats, the Government enlists the disposable Task Force X on a high-risk mission in exchange for absolution: Meanwhile, the Joker operates his own agenda.", 
       "Suicide Squad.mp4", "Action, Adventure, Crime", " Will Smith, Jared Leto, Margot Robbie ")); 

     movies.add(new MovieDescription("Sausage Party", "R", "12 August 2016", "1h 29min", 
       "The products at Shopwell's Grocery Store are made to believe a code that helps them live happy lives until it's time for them to leave the comfort of the supermarket and head for the great beyond. However, after a botched trip to the great beyond leaves one sausage named Frank and his companion Bun stranded, Frank goes to great lengths (pun intended) to return to his package and make another trip to the great beyond. But as Frank's journey takes him from one end of the supermarket to the other, Frank's quest to discover the truth about his existence as a sausage turns incredibly dark. Can he expose the truth to the rest of the supermarket and get his fellow products to rebel against their human masters?", 
       "Sausage Party.mp4", "Animation, Adventure, Comedy", " Seth Rogen, Kristen Wiig, Jonah Hill")); 

     movies.add(new MovieDescription("Deadpool", "R", "12 February 2016", "1h 48min", 
       "A former Special Forces operative turned mercenary is subjected to a rogue experiment that leaves him with accelerated healing powers, adopting the alter ego Deadpool. ", 
       "Deadpool.mp4", "Action, Adventure, Comedy", " Ryan Reynolds, Morena Baccarin, T.J. Miller")); 

     movies1.printLibrary(); 

     PrintWriter out = new PrintWriter("moviesJava.json"); 

     out.println(movies1.toJSONString()); 

     out.close(); 

     System.out.println("Done exporting group in json to movies.json"); 

     // now use java's built in serialization to serialize and 
     // deserialize 

     File outFile = new File("movies.ser"); 

     ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(outFile)); 

     os.writeObject(movies); 

     os.flush(); 

     System.out.println("Used Java serialization of the group to movies.ser"); 

     os.close(); 

     File inFile = new File("movies.ser"); 

     ObjectInputStream in = new ObjectInputStream(new FileInputStream(inFile)); 

     MovieLibrary movieAgain = (MovieLibrary) in.readObject(); 

     System.out.println("Done importing the group from movies.ser as:"); 

     movieAgain.printLibrary(); 

     in.close(); 

    } catch (Exception e) { 

     System.out.println("exception: " + e.getMessage()); 

     e.printStackTrace(); 

    } 

} 

、これはtoJsonString方法

public String toJSONString() { 

    String ret; 

    JSONObject obj = new JSONObject(); 

    for (Enumeration<MovieDescription> e = libraryList.elements(); e.hasMoreElements();) { 

     MovieDescription movi = (MovieDescription) e.nextElement(); 

     try { 

      obj.put(movi.getTitle(), movi.toJson()); 
      /* 
      * obj.put(movi.getActors(),movi.toJson()); 
      * obj.put(movi.getFilename(),movi.toJson()); 
      * obj.put(movi.getGenre(),movi.toJson()); 
      * obj.put(movi.getPlot(),movi.toJson()); 
      * obj.put(movi.getRated(),movi.toJson()); 
      * obj.put(movi.getReleased(),movi.toJson()); 
      * obj.put(movi.getRuntime(),movi.toJson()); 
      */ 

     } catch (JSONException e1) { 

      e1.printStackTrace(); 

     } 

    } 

    ret = obj.toString(); 

    return ret; 

} 

である私は、次の例外を取得しています:

org.json.JSONException: Null key. 
at org.json.JSONObject.put(JSONObject.java:1097) 
at MovieLibrary.toJSONString(MovieLibrary.java:124) 
at MovieLibrary.main(MovieLibrary.java:69) 
+0

あなたはライン124と 'MovieLibrary.java' –

+2

69' movi.getTitleは() 'nullを返すように見えるあなたのコードで示すことができます。それを確認できますか? – BNilsou

+0

そのかなり明白なtoJSONStringメソッドのputコマンドです。あなたが 'movi.getTitle()'にヌルを持っていると思います。 –

答えて

0

これを行うためのクリーンな方法は、ジャクソンを使用することです。

クラス内に各変数のgetterメソッドとsetterメソッドを使用してMovieLibraryクラスを作成します。

ObjectMapper mapper = new ObjectMapper(); 
MovieLibrary movies = new MovieLibrary(); 
//Object to JSON in file 
mapper.writeValue(new File("c:\\file.json"), movies); 

上記のコードは、ファイルのデータをMovieLibraary(movies)のオブジェクトにマップします。 setterメソッドを使用してムービーオブジェクトの値を変更できます。

http://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/

関連する問題