简体   繁体   中英

Reading a JSON file with java

I'm having a problem in reading the "Email" field from a JSON file, using Java. When I try to read it, it only reads the first one, even if I put more than one, I tried different things but nothing seems to go. Any way to solve it? Here is the code:

This is the sign in method, where I write every data about the customer on the JSON file

JSONObject customer = new JSONObject();
JSONArray list = new JSONArray();   
customer.put("Email", emailCliente.getText());
customer.put("Tipo", "Cliente");
customer.put("Name", nomeCliente.getText());
customer.put("Surname", cognomeCliente.getText());
customer.put("BirthDate", dataNascitaCliente.getText());
customer.put("Address", indirizzoCliente.getText());
customer.put("Phone", telefonoCliente.getText());
customer.put("Password", pswCliente.getText());

list.add(customer);

try {
    FileOutputStream file = new FileOutputStream("Db.json", true);
    ObjectOutputStream fileWriter = new ObjectOutputStream(file);
    fileWriter.writeObject(list);
    fileWriter.flush();
    fileWriter.close(); 
}

This is the code for reading from JSON file:

public class Read implements Serializable{
public static void main(String[] args) throws IOException {
    try{
        FileInputStream reader = new FileInputStream("Db.json");
        ObjectInputStream ois = new ObjectInputStream(reader);
        Object customer = (Object) ois.readObject();
        JSONArray tmp = (JSONArray) (customer);
        for(Object obj : tmp) {
            JSONObject tmpObj = (JSONObject) obj;
            System.out.println(tmpObj.get("Email"));
        }
        ois.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
} }

You're using the org.json library to create and read JSON data.

Don't. That library is deplorably bad.

I know json.org lists it.

An excellent choice is jackson , or perhaps gson if you want an alternative. As @Raúl Garcia mentioned in a comment, here is a good baeldung tutorial on jackson .

NB: DataInputStream and DataOutputStream are for java's serialization mechanism, which isn't JSON and you don't want those in any case, so, throw out your 'read' code and start from scratch, by following the tutorial. Also, your exception code is problematic; exceptions contain 4 bits of info (type, message, trace, and cause); you're throwing out 3 of the 4 useful bits of info then blindly continuing, which likely produces more clutter in your logs, making it extremely difficult to try to figure out what is going wrong. Stop doing this; just 'throws' such exceptions onwards. If you really, really can't, fix your IDE to generate catch (Foo e) {throw new RuntimeException(e);} as a default catch block. NEVER e.printStackTrace(); .

You could read it a single line like so when you use unify-jdocs:

Document d = new JDocument(s); // where s is a JSON string
String s = d.getString("$.Email");

unify-jdocs is a library I have created to work with JSON documents. It provides a whole lot of other features as well. Check it out on https://github.com/americanexpress/unify-jdocs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM