2016-09-12 4 views
0

Ich versuche, eine JSON-Datei zu importieren und die Daten innerhalb sie verwenden und die Datei als jsonmit JSON-Datei in Java

Verwenden Sie die Datei movies.json zu initialisieren die Filmbibliothek und exportieren eine modifizierte Json-Datei Datei exportieren Speichern Sie die Bibliothek nach Abschluss des Programms.

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(); 

    } 

} 

und dies ist die toJsonString Methode

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; 

} 

ich die folgende Ausnahme erhalten:

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

können Sie in Ihrem Code angeben, wo die Leitung 124 und 69 in 'MovieLibrary.java' –

+2

Es scheint, dass' movi.getTitle() 'null zurückgibt. Kannst du das verifizieren? – BNilsou

+0

ist es ziemlich offensichtlich, es ist in der Put-Befehl in toJSONString-Methode. Ich denke, Sie haben null in 'movi.getTitle()' –

Antwort

0

Ein sauberere Weg, dies zu tun ist durch Jackson verwenden.

Erstellen Sie eine MovieLibrary-Klasse mit Getter- und Setter-Methoden für jede darin enthaltene Variable.

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

Der obige Code wird die Daten in der Datei zum Objekt von MovieLibrary (Filme) zuordnen. Sie können den Wert im Filmobjekt mithilfe von Setter-Methoden ändern.

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