Я хотел бы создать JSONObject, который содержит всю информацию в JSONFile.
Я искал в интернете и только нашел, как добавить одного человека в этот объект. Я хотел бы знать, есть ли способ добавить всех людей с их атрибутами в один JSONObject.
public class JsonSingleton extends AbstractSingleton {
final String personnesJsonPathFile = "personnesJson.json";
private JsonSingleton() {
}
public Personne createPersonneWithFileObject( JSONObject jsonObjectPerson ) {
int idPersonne = Integer.parseInt( ( String ) jsonObjectPerson.get( "id" ) );
String prenom = ( String ) jsonObjectPerson.get( "prenom" );
String nom = ( String ) jsonObjectPerson.get("nom");
Double poids = Double.parseDouble( ( String ) jsonObjectPerson.get( "poids" ) );
Double taille = Double.parseDouble( ( String ) jsonObjectPerson.get( "taille" ) );
String rue = ( String ) jsonObjectPerson.get( "rue" );
String ville = ( String ) jsonObjectPerson.get( "ville" );
String codePostal = ( String ) jsonObjectPerson.get( "codePostal" );
return new Personne( idPersonne, prenom, nom, poids, taille, rue, ville, codePostal );
}
@Override
protected void extractPersonnesDatas() {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse( new FileReader ( personnesJsonPathFile ) );
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
}
Вот данные Json:
{"personnes":[{"id":1,"prenom":"Martin","nom":"Marshall","poids":60,"taille":150,"rue":"rue de Nantes","ville":"Laval","codePostal":"53000"},
{"id":2,"prenom":"Claire","nom":"Chazal","poids":65,"taille":155,"rue":"rue de Rennes","ville":"Laval","codePostal":"53000"},
{"id":3,"prenom":"Jacques","nom":"Dupont","poids":90,"taille":180,"rue":"rue des Anges","ville":"Paris","codePostal":"75000"},
{"id":4,"prenom":"Celine","nom":"Dia","poids":66,"taille":166,"rue":"rue Diderot","ville":"Paris","codePostal":"75000"},
{"id":5,"prenom":"Remy","nom":"Cheval","poids":88,"taille":200,"rue":"rue du paradis","ville":"Nantes","codePostal":"44000"},
{"id":6,"prenom":"Nicolas","nom":"Dutrou","poids":40,"taille":150,"rue":"rue Appert","ville":"Nantes","codePostal":"44000"},
{"id":7,"prenom":"Marie","nom":"Claire","poids":92,"taille":188,"rue":"rue du paradis","ville":"Rouen","codePostal":"76000"},
{"id":8,"prenom":"Nathalie","nom":"Sage","poids":75,"taille":175,"rue":"rue Appert","ville":"Rouen","codePostal":"76000"},
{"id":9,"prenom":"Jean","nom":"Dujardin","poids":56,"taille":166,"rue":"rue des sorciers","ville":"Havre","codePostal":"76800"},
{"id":10,"prenom":"Michel","nom":"Leclerc","poids":100,"taille":202,"rue":"rue du bonneur","ville":"Havre","codePostal":"76800"},
{"id":11,"prenom":"Julien","nom":"Marshall","poids":65,"taille":144,"rue":"rue de Nantes","ville":"Laval","codePostal":"53000"},
{"id":12,"prenom":"Julien","nom":"Claire","poids":85,"taille":169,"rue":"rue du Paradis","ville":"Paris","codePostal":"75000"},
{"id":13,"prenom":"Jacques","nom":"Dupont","poids":87,"taille":172,"rue":"rue des Passeurs","ville":"Paris","codePostal":"75000"},
{"id":14,"prenom":"Charles","nom":"Hallyday","poids":69,"taille":133,"rue":"rue des Feugrais","ville":"Rouen","codePostal":"76000"},
{"id":15,"prenom":"Serge","nom":"Lama","poids":78,"taille":174,"rue":"rue des Heureux","ville":"Nantes","codePostal":"44000"},
{"id":16,"prenom":"Vincent","nom":"Thomas","poids":35,"taille":169,"rue":"rue de Paris","ville":"Rennes","codePostal":"35000"},
{"id":17,"prenom":"Eric","nom":"Dummat","poids":56,"taille":155,"rue":"rue de Versaille","ville":"Paris","codePostal":"75000"},
{"id":18,"prenom":"Nicolas","nom":"Samuel","poids":49,"taille":171,"rue":"rue de Saint Louis","ville":"Laval","codePostal":"53000"},
{"id":19,"prenom":"Rémy","nom":"Guerry","poids":54,"taille":157,"rue":"rue des Sages","ville":"Lyon","codePostal":"69000"},
{"id":20,"prenom":"Nicolas","nom":"Drapeau","poids":63,"taille":145,"rue":"rue Mitterrand","ville":"Limoges","codePostal":"87000"}
]}