英文:
Java parsed JSON array (GSON) gives back zero or null
问题
你的代码看起来几乎正确,但是在读取文件并将其转换为 JSON 对象数组时出现了一些问题。在你的代码中,你只是将整个文件的内容作为一个字符串读取,并尝试将这个字符串转换为 JSON 数组。然而,实际上你的文件内容是一个包含多个 JSON 对象的数组。
你应该逐行读取文件,然后将每一行解析为一个单独的 JSON 对象,然后将这些对象放入数组中。以下是你应该更改的部分代码:
Gson gson = new Gson();
List<Messung> messungenList = new ArrayList<>(); // 使用列表来暂存 Messung 对象
while (myReader.hasNextLine()) {
String line = myReader.nextLine();
Messung messung = gson.fromJson(line, Messung.class); // 将每一行解析为 Messung 对象
messungenList.add(messung);
}
Messung[] messungen = messungenList.toArray(new Messung[0]);
for (Messung messung : messungen) {
System.out.println(messung);
}
这将逐行读取文件,将每一行解析为一个 Messung 对象,然后将这些对象添加到列表中。最后,你可以将列表转换为数组,并遍历数组打印每个 Messung 对象。
请确保在你的 Messung
类中,属性名称与 JSON 中的字段名称匹配,这样 GSON 才能正确地将 JSON 数据映射到对象属性。
英文:
I have a JSON array that looks like this:
[
{
"ScanDateTime": "2020-10-11T00:04:05",
"RecipeName": "_BLANK_16",
"Slot": 13,
"DefectTotal": 0,
"AreaCount": 3,
"Part1": 0,
"Part2": 0,
"Part3": 0,
"Part4": 0,
"Part5": 0,
"Part6": 0,
"Part7": 0,
"Part8": 0,
"HazeRegion": 0,
"HazeAverage": 0,
"HazePeak": 0,
"TotalScratch": 0
},
{
"ScanDateTime": "2020-10-11T00:34:36",
"RecipeName": "_BLANK_16",
"Slot": 24,
"DefectTotal": 50,
"AreaCount": 5,
"Part1": 21,
"Part2": 25,
"Part3": 0,
"Part4": 1,
"Part5": 1,
"Part6": 2,
"Part7": 0,
"Part8": 0,
"HazeRegion": 0,
"HazeAverage": 0,
"HazePeak": 0,
"TotalScratch": 0
},
{
"ScanDateTime": "2020-10-11T00:35:26",
"RecipeName": "_BLANK_16",
"Slot": 23,
"DefectTotal": 192,
"AreaCount": 2401,
"Part1": 34,
"Part2": 63,
"Part3": 30,
"Part4": 16,
"Part5": 9,
"Part6": 40,
"Part7": 0,
"Part8": 0,
"HazeRegion": 0,
"HazeAverage": 0,
"HazePeak": 0,
"TotalScratch": 39.205788
},
{
"ScanDateTime": "2020-10-11T03:05:29",
"RecipeName": "_BLANK_16",
"Slot": 0,
"DefectTotal": 945,
"AreaCount": 57,
"Part1": 273,
"Part2": 479,
"Part3": 93,
"Part4": 33,
"Part5": 35,
"Part6": 32,
"Part7": 0,
"Part8": 0,
"HazeRegion": 0,
"HazeAverage": 0,
"HazePeak": 0,
"TotalScratch": 0
},
{
"ScanDateTime": "2020-10-11T03:06:55",
"RecipeName": "_BLANK_16",
"Slot": 4,
"DefectTotal": 842,
"AreaCount": 78,
"Part1": 203,
"Part2": 469,
"Part3": 95,
"Part4": 33,
"Part5": 13,
"Part6": 29,
"Part7": 0,
"Part8": 0,
"HazeRegion": 0,
"HazeAverage": 0,
"HazePeak": 0,
"TotalScratch": 0
},
{
"ScanDateTime": "2020-10-11T03:07:44",
"RecipeName": "_BLANK_16",
"Slot": 9,
"DefectTotal": 391,
"AreaCount": 56,
"Part1": 125,
"Part2": 214,
"Part3": 25,
"Part4": 6,
"Part5": 11,
"Part6": 10,
"Part7": 0,
"Part8": 0,
"HazeRegion": 0,
"HazeAverage": 0,
"HazePeak": 0,
"TotalScratch": 0
}
]
I use GSON to parse it and have an class Messung
that looks like this:
public class Messung {
private String scanDateTime;
private String recipeName;
private int slot;
private int defectTotal;
private int areaCount;
private int part1;
private int part2;
private int part3;
private int part4;
private int part5;
private int part6;
private int part7;
private int part8;
private double hazeRegion;
private double hazePeak;
private double totalScratch;
public Messung(String scanDateTime, String recipeName, int slot, int defectTotal1, int areaCount1, int part1, int part2, int part3, int part4, int part5, int part6, int part7, int part8, double hazeRegion, double hazePeak, double totalScratch){
this.scanDateTime = scanDateTime;
this.recipeName = recipeName;
this.slot = slot;
this.defectTotal = defectTotal1;
this.areaCount = areaCount1;
this.part1 = part1;
this.part2 = part2;
this.part3 = part3;
this.part4 = part4;
this.part5 = part5;
this.part6 = part6;
this.part7 = part7;
this.part8 = part8;
this.hazeRegion = hazeRegion;
this.hazePeak = hazePeak;
this.totalScratch = totalScratch;
}
public String getScanDateTime() {
return scanDateTime;
}
public String getRecipeName() {
return recipeName;
}
public int getSlot() {
return slot;
}
public int getDefectTotal() {
return defectTotal;
}
public int getAreaCount() {
return areaCount;
}
public int getPart1() {
return part1;
}
public int getPart2() {
return part2;
}
public int getPart3() {
return part3;
}
public int getPart4() {
return part4;
}
public int getPart5() {
return part5;
}
public int getPart6() {
return part6;
}
public int getPart7() {
return part7;
}
public int getPart8() {
return part8;
}
public double getHazeRegion() {
return hazeRegion;
}
public double getHazePeak() {
return hazePeak;
}
public double getTotalScratch() {
return totalScratch;
}
@Override
public String toString() {
return "Messung [ScanDateTime=" + scanDateTime + ", RecipeName=" + recipeName + ", Slot=" + slot + ", DefectTotal=" + defectTotal + ", AreaCount=" + areaCount + ", Part1=" + part1 + ", Part2=" + part2 + ", Part3=" + part3 + ", Part4=" + part4 + ", Part5=" + part5 + ", Part6=" + part6 + ", Part7=" + part7 + ", Part8=" + part8 + ", HazeRegion=" + hazeRegion + ", HazePeak=" + hazePeak + ", TotalScratch=" + getTotalScratch() + "]";
}
}
This is the part of my script where I try to parse it:
while(!isExisting){
try {
File myObj = new File("C:\\workspace\\AccessDBReader-6420-II\\AccessDBReader-6420-II\\bin\\Release\\Publish\\Messung-6420-II.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
data = myReader.nextLine();
}
System.out.println(data);
myReader.close();
Gson gson = new Gson();
Messung[] messungen = gson.fromJson(data, Messung[].class);
for(Messung messung : messungen) {
System.out.println(messung);
}
isExisting = true;
} catch (FileNotFoundException e) {
Thread.sleep(10000);
//e.printStackTrace();
}
But when I try to print the Messung-objects I get null
for Strings and 0 for int
and double
:
Messung [ScanDateTime=null, RecipeName=null, Slot=0, DefectTotal=0, AreaCount=0, Part1=0, Part2=0, Part3=0, Part4=0, Part5=0, Part6=0, Part7=0, Part8=0, HazeRegion=0.0, HazePeak=0.0, TotalScratch=0.0]
Messung [ScanDateTime=null, RecipeName=null, Slot=0, DefectTotal=0, AreaCount=0, Part1=0, Part2=0, Part3=0, Part4=0, Part5=0, Part6=0, Part7=0, Part8=0, HazeRegion=0.0, HazePeak=0.0, TotalScratch=0.0]
Messung [ScanDateTime=null, RecipeName=null, Slot=0, DefectTotal=0, AreaCount=0, Part1=0, Part2=0, Part3=0, Part4=0, Part5=0, Part6=0, Part7=0, Part8=0, HazeRegion=0.0, HazePeak=0.0, TotalScratch=0.0]
Messung [ScanDateTime=null, RecipeName=null, Slot=0, DefectTotal=0, AreaCount=0, Part1=0, Part2=0, Part3=0, Part4=0, Part5=0, Part6=0, Part7=0, Part8=0, HazeRegion=0.0, HazePeak=0.0, TotalScratch=0.0]
Messung [ScanDateTime=null, RecipeName=null, Slot=0, DefectTotal=0, AreaCount=0, Part1=0, Part2=0, Part3=0, Part4=0, Part5=0, Part6=0, Part7=0, Part8=0, HazeRegion=0.0, HazePeak=0.0, TotalScratch=0.0]
Messung [ScanDateTime=null, RecipeName=null, Slot=0, DefectTotal=0, AreaCount=0, Part1=0, Part2=0, Part3=0, Part4=0, Part5=0, Part6=0, Part7=0, Part8=0, HazeRegion=0.0, HazePeak=0.0, TotalScratch=0.0]
Messung [ScanDateTime=null, RecipeName=null, Slot=0, DefectTotal=0, AreaCount=0, Part1=0, Part2=0, Part3=0, Part4=0, Part5=0, Part6=0, Part7=0, Part8=0, HazeRegion=0.0, HazePeak=0.0, TotalScratch=0.0]
Messung [ScanDateTime=null, RecipeName=null, Slot=0, DefectTotal=0, AreaCount=0, Part1=0, Part2=0, Part3=0, Part4=0, Part5=0, Part6=0, Part7=0, Part8=0, HazeRegion=0.0, HazePeak=0.0, TotalScratch=0.0]
What am I doing wrong?
答案1
得分: 1
字符大小写不匹配。在你的Java类中,你使用了:
scanDateTime
而你的JSON文件中使用了:
ScanDateTime
我建议你尝试在你的Java类或者JSON文件中更新大小写。
英文:
The character-casing doesn't match. In your java class you use:
> scanDateTime
while your json file uses:
> ScanDateTime
I'd try to update the case in either your java-class or the json file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论