简体   繁体   English

从 Json 到 Object、Java

[英]From Json to Object, Java

i have an Array of json objects that looks like this (but has like 30k lines):我有一个 json 对象数组,看起来像这样(但有 30k 行):

{
    "Foglio1": [
        {
            "istat": "1001",
            "cap": "10011"
        },
        {
            "istat": "1002",
            "cap": "10060"
        },
        {
            "istat": "1003",
            "cap": "10070"
        },
        {
            "istat": "1004",
            "cap": "10010"
        },
        {
            "istat": "1006",
            "cap": "10040"
        }
     ]
}

my classes:我的课程:

public class Test {

    public static void main(String[] args) throws IOException {
        BufferedReader reader=new BufferedReader(new FileReader("italy_cap.json"));
        String cap=reader.readLine();
        Gson g = new Gson();
        Foglio1 p=null;
        while(cap!=null) {
            p = g.fromJson(cap, Foglio1.class);
            
            cap=reader.readLine();
        }
        System.out.println(p);
        
        
    }

}
public class Foglio1 {
    ArrayList <Cap> Foglio1;
}
public class Cap {
    String istat;
    String cap;
    
    public String getIstat() {
        return istat;
    }
    public void setIstat(String istat) {
        this.istat = istat;
    }
    public String getCap() {
        return cap;
    }
    public void setCap(String cap) {
        this.cap = cap;
    }
}

I wanted to read the json and convert it into a java object for parsing it, but i'm not getting how to make it work.我想阅读 json 并将其转换为 java object 以进行解析,但我不知道如何使其工作。 the error it's giving me it's:它给我的错误是:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 14 path $.Foglio1[0]
    at com.google.gson.Gson.fromJson(Gson.java:937)
    at com.google.gson.Gson.fromJson(Gson.java:892)
    at com.google.gson.Gson.fromJson(Gson.java:841)
    at com.google.gson.Gson.fromJson(Gson.java:813)
    at net.codejava.ws.Test.main(Test.java:21)
Caused by: java.io.EOFException: End of input at line 1 column 14 path $.Foglio1[0]
    at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1401)
    at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:549)
    at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:414)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
    at `enter code here`com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
    at com.google.gson.Gson.fromJson(Gson.java:927)

As alternative can you give me any ideas of libraries for big json?.作为替代方案,你能给我关于大 json 库的任何想法吗?

Thank you for the help:)感谢您的帮助:)

JSON is not a line-oriented data format, and trying to read it line by line isn't going to work. JSON 不是面向行的数据格式,尝试逐行读取它是行不通的。 You can just give the entire file to Gson all at once:您可以一次将整个文件提供给 Gson:

static void main(String[] args) throws IOException {
    try (BufferedReader reader=new BufferedReader(new FileReader("italy_cap.json"))) {
        Gson g = new Gson();
        Foglio1 p = g.fromJson(reader, Foglio1.class);
        System.out.println(p);
    }
}

If your JSON is so big that the objects you create from it don't fit in memory, you can switch to reading it as a stream , but that may require extensive changes to the rest of your program. If your JSON is so big that the objects you create from it don't fit in memory, you can switch to reading it as a stream , but that may require extensive changes to the rest of your program. For example, you may have to change the list within the Foglio1 class to something else, or get rid of the Foglio1 class entirely.例如,您可能必须将Foglio1 class 中的列表更改为其他内容,或者完全删除Foglio1 class。 Without knowing the rest of your program, it's impossible to say.在不知道你程序的rest的情况下,是不可能说出来的。

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

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