从Json到对象,Java

huangapple go评论80阅读模式
英文:

From Json to Object, Java

问题

我有一个JSON对象数组,看起来像这样(但有大约30,000行):

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

我的类:

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;
    }
}

我想读取JSON并将其转换为Java对象以进行解析,但我不知道如何使其工作。它给我报错:

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 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
    at com.google.gson.Gson.fromJson(Gson.java:927)

作为替代方案,您可以考虑使用用于处理大型JSON的库。感谢您的帮助 从Json到对象,Java

英文:

i have an Array of json objects that looks like this (but has like 30k lines):

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

my classes:

public class Test {
public static void main(String[] args) throws IOException {
BufferedReader reader=new BufferedReader(new FileReader(&quot;italy_cap.json&quot;));
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 &lt;Cap&gt; 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.
the error it's giving me it's:

Exception in thread &quot;main&quot; 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?.

Thank you for the help 从Json到对象,Java

答案1

得分: 0

JSON不是一个面向行的数据格式,尝试逐行读取它是行不通的。你可以将整个文件一次性交给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);
    }
}

如果你的JSON太大,无法一次性加载到内存中,你可以切换到以流的方式读取,但这可能需要对你程序的其他部分进行广泛的更改。例如,你可能需要将Foglio1类中的列表更改为其他数据结构,或者完全摒弃Foglio1类。没有了解你程序的其他部分,无法确定具体的操作。

英文:

JSON is not a line-oriented data format, and trying to read it line by line isn't going to work. You can just give the entire file to Gson all at once:

static void main(String[] args) throws IOException {
try (BufferedReader reader=new BufferedReader(new FileReader(&quot;italy_cap.json&quot;))) {
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. For example, you may have to change the list within the Foglio1 class to something else, or get rid of the Foglio1 class entirely. Without knowing the rest of your program, it's impossible to say.

huangapple
  • 本文由 发表于 2020年7月31日 23:50:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63195111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定