gpt4 book ai didi

java - Gson.json() 解析json文件时返回null

转载 作者:行者123 更新时间:2023-11-30 10:01:40 27 4
gpt4 key购买 nike

我正在尝试将 JSON 文件(我已经创建的)解析为 java 对象。

我尝试过使用 JsonReader 和 bufferedReader 但每次尝试都返回 null

public class Sheep implements Serializable {

@SerializedName("localId")
@Expose
private int localId;
@SerializedName("globalId")
@Expose
private int globalId;
@SerializedName("birthDate")
@Expose
private Date birthDate = null;
@SerializedName("sellingDate")
@Expose
private Date sellingDate = null;
@SerializedName("deathDate")
@Expose
private Date deathDate = null;
@SerializedName("lastpregnancyDate")
@Expose
private Date lastpregnancyDate = null;
@SerializedName("lastDoctorCheck")
@Expose
private Date lastDoctorCheck = null;
@SerializedName("alive")
@Expose
private boolean alive;
@SerializedName("sold")
@Expose
private boolean sold;
@SerializedName("pregnant")
@Expose
private boolean pregnant;
@SerializedName("mother")
@Expose
private int mother;
@SerializedName("father")
@Expose
private int father;
@SerializedName("husband")
@Expose
private int husband;
@SerializedName("allDoctorChecks")
@Expose
private List<Date> allDoctorChecks = null;
@SerializedName("allpregnancies")
@Expose
private List<Date> allpregnancies = null;
@SerializedName("children")
@Expose
private List<Integer> children = null;
@SerializedName("allHusbands")
@Expose
private List<Integer> allHusbands = null;
}

public class Farm implements Serializable {

@SerializedName("allSheeps")
@Expose
private List<Sheep> allSheeps;
}

在这里您可以看到我尝试解析 JSON 的两种方法。其实我也不知道这两者到底有什么区别,我只是在类似问题的某人的回答中看到了第二个

1.

public class DataBase {
private static Farm farm;

public static void main(String[] args){
...

Gson gson = new Gson();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("farm.json"));
Farm parsedFarm = gson.fromJson(br, Farm.class);
System.out.println(parsedFarm);
}catch(Exception e){
System.out.println(e.fillInStackTrace());
}
...

}

2.

public class DataBase {
private static Farm farm;

public static void main(String[] args){
...

Gson gson = new Gson();
JsonReader jsonReader = null;
Type farmType = new TypeToken<List<Sheep>>(){}.getType();
try {
jsonReader = new JsonReader(new FileReader("farm.json"));
List<Sheep> parsedFarm = gson.fromJson(jsonReader , farmType);
System.out.println(parsedFarm);
}catch(Exception e){
System.out.println(e.fillInStackTrace());
}
...

}

在对这两种方法的解析结束时,我得到:

java.lang.NullPointerException


{
"allSheeps": [
{
"localId": 0,
"globalId": 10,
"birthDate": "Jan 12, 3913, 12:00:00 AM",
"alive": true,
"sold": false,
"pregnant": false,
"mother": -1,
"father": -1,
"husband": -1
},
{
"localId": 1,
"globalId": 20,
"birthDate": "Jan 12, 3913, 12:00:00 AM",
"alive": true,
"sold": false,
"pregnant": false,
"mother": -1,
"father": -1,
"husband": -1
},
{
"localId": 2,
"globalId": 200,
"birthDate": "Jan 12, 3913, 12:00:00 AM",
"alive": true,
"sold": false,
"pregnant": false,
"mother": 0,
"father": 1,
"husband": -1
},
{
"localId": 3,
"globalId": 300,
"birthDate": "Jan 12, 3913, 12:00:00 AM",
"alive": true,
"sold": false,
"pregnant": false,
"mother": 1,
"father": 2,
"husband": -1
},
{
"localId": 4,
"globalId": 400,
"birthDate": "Jan 12, 3913, 12:00:00 AM",
"alive": true,
"sold": false,
"pregnant": false,
"mother": 3,
"father": 3,
"husband": -1
}
]
}

完整的 stacetrace:


"C:\Program Files\Java\jdk-12.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.2\lib\idea_rt.jar=49271:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\saleh\Desktop\farm\out\production\farm;C:\Users\saleh\.m2\repository\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar com.company.DB.DataBase
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.gson.internal.reflect.UnsafeReflectionAccessor (file:/C:/Users/saleh/.m2/repository/com/google/code/gson/gson/2.8.5/gson-2.8.5.jar) to field java.text.SimpleDateFormat.serialVersionOnStream
WARNING: Please consider reporting this to the maintainers of com.google.gson.internal.reflect.UnsafeReflectionAccessor
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Exception in thread "main" java.lang.NullPointerException
at com.company.Farm.Sheep.toString(Sheep.java:237)
at java.base/java.lang.String.valueOf(String.java:3042)
at java.base/java.lang.StringBuilder.append(StringBuilder.java:168)
at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:473)
at java.base/java.lang.String.valueOf(String.java:3042)
at java.base/java.lang.StringBuilder.append(StringBuilder.java:168)
at com.company.Farm.Farm.toString(Farm.java:60)
at java.base/java.lang.String.valueOf(String.java:3042)
at java.base/java.io.PrintStream.println(PrintStream.java:897)
at com.company.DB.DataBase.main(DataBase.java:66)

Process finished with exit code 1

最佳答案

好吧,在这两种方法中,您都只初始化了文件读取对象,但还没有开始读取它。

在方法 1 中,如果你想读取文件,你应该使用类似下面的内容。

String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}

然后你必须解析字符串line。在您的情况下,实际上没有从文件中读取任何内容来解析,这就是它返回空异常的原因。无论如何,我不能保证这种方法是否可以使用 Gson 解析,因为它是逐行读取而不是一次读取 Json 节点

因此,如果您再次犯了与上述相同的错误,则第二种方法会更方便。请参阅随附的链接 ( Gson - JsonReader Doc ) 以更好地理解方法 2。

编辑:下面提到了与从 link 中提取的原始问题的第二种方法相关的示例代码片段。无论如何,如果它可能不再起作用。

String json = "{\"brand\" : \"Toyota\", \"doors\" : 5}";

JsonReader jsonReader = new JsonReader(new StringReader(json));

try {
while(jsonReader.hasNext()){
JsonToken nextToken = jsonReader.peek();
System.out.println(nextToken);

if(JsonToken.BEGIN_OBJECT.equals(nextToken)){

jsonReader.beginObject();

} else if(JsonToken.NAME.equals(nextToken)){

String name = jsonReader.nextName();
System.out.println(name);

} else if(JsonToken.STRING.equals(nextToken)){

String value = jsonReader.nextString();
System.out.println(value);

} else if(JsonToken.NUMBER.equals(nextToken)){

long value = jsonReader.nextLong();
System.out.println(value);

}
}
} catch (IOException e) {
e.printStackTrace();
}

关于java - Gson.json() 解析json文件时返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57309682/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com