_PWR_Bruce a écrit: |
Code: |
List<Media> listMedia; JSONObject json; protected void sauvegardeMediatheque() throws FileNotFoundException { this.json = new JSONObject(); Map m = new LinkedHashMap(); for(Media media : this.listMedia) { m.put("idMedia", media.idMedia); m.put("titre", media.titreMedia); m.put("genre", media.genreMedia); m.put("maisonProduction", media.maisonProductionMedia); m.put("nbExemplaire", media.nombreExemplaire); } Iterator<Map.Entry> itr1 = m.entrySet().iterator(); while (itr1.hasNext()) { Map.Entry pair = itr1.next(); System.out.println(pair.getKey() + " : " + pair.getValue()); } json.put("Media", m); // writing JSON to file:"JSONExample.json" in cwd PrintWriter pw = new PrintWriter("JSONExample.json"); pw.write(json.toJSONString()); pw.flush(); pw.close(); } protected void contenuJson() throws FileNotFoundException, IOException, ParseException { // parsing file "JSONExample.json" Object obj = new JSONParser().parse(new FileReader("JSONExample.json")); // typecasting obj to JSONObject JSONObject jo = (JSONObject) obj; // getting address Map media = ((Map)jo.get("Media")); // iterating address Map Iterator<Map.Entry> itr1 = media.entrySet().iterator(); while (itr1.hasNext()) { Map.Entry pair = itr1.next(); System.out.println(pair.getKey() + " : " + pair.getValue()); } } |
Sujets similaires |
|||||
Sujet | Auteur | Forum | Réponses | Posté le | |
---|---|---|---|---|---|
Et me voilà sur le Forum | Jess | Présentation | 0 | 04 février 2024 à 19:12 | |
Un vieux DVD de demo joypad, quelqu'un d'interressé? | Pouetster | Le poulailler | 0 | 09 décembre 2023 à 19:53 | |
JAVA DK 19 Crash | M0rPh3u5 | Développement | 3 | 18 mars 2023 à 14:38 | |
Je suis donc nouveau ! pas par mon âge, mais sur le forum... | youn29 | Présentation | 0 | 05 décembre 2022 à 17:01 | |
slt ! carte mère express quelqu'un connais ? | enflag143 | CPU, Cartes mères, RAM | 10 | 01 octobre 2022 à 23:44 |