简体   繁体   English

用java读取JSON文件

[英]Reading a JSON file with java

I'm having a problem in reading the "Email" field from a JSON file, using Java.我在使用 Java 从 JSON 文件中读取“电子邮件”字段时遇到问题。 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这是登录方法,我将有关客户的所有数据写入 JSON 文件

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:这是从 JSON 文件中读取的代码:

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.您正在使用org.json库来创建和读取 JSON 数据。

Don't.别。 That library is deplorably bad.那个图书馆非常糟糕。

I know json.org lists it.我知道json.org列出了它。

An excellent choice is jackson , or perhaps gson if you want an alternative.一个很好的选择是jackson ,或者gson如果你想要一个替代品。 As @Raúl Garcia mentioned in a comment, here is a good baeldung tutorial on jackson .正如@Raúl Garcia 在评论中提到的,这里有一个关于 jackson 的很好的 baeldung 教程

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.注意: DataInputStreamDataOutputStream用于 Java 的序列化机制,它不是 JSON,在任何情况下您都不需要它们,因此,按照教程,扔掉您的“读取”代码并从头开始。 Also, your exception code is problematic;此外,您的异常代码有问题; exceptions contain 4 bits of info (type, message, trace, and cause);异常包含 4 位信息(类型、消息、跟踪和原因); 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.您丢弃了 4 个有用的信息中的 3 个,然后盲目地继续,这可能会在您的日志中产生更多的混乱,使得尝试找出问题所在变得极其困难。 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.如果你真的,真的不能,修复你的 IDE 以生成catch (Foo e) {throw new RuntimeException(e);}作为默认的 catch 块。 NEVER e.printStackTrace();永远不要e.printStackTrace(); . .

You could read it a single line like so when you use unify-jdocs:当您使用 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. unify-jdocs 是我创建的用于处理 JSON 文档的库。 It provides a whole lot of other features as well.它还提供了许多其他功能。 Check it out on https://github.com/americanexpress/unify-jdocshttps://github.com/americanexpress/unify-jdocs上查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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