Java解析的JSON数组(GSON)返回零或空值

qybjjes1  于 2022-11-06  发布在  Java
关注(0)|答案(1)|浏览(230)

我有一个JSON数组,如下所示:

[
  {
    "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
  }
]

我使用GSON来解析它,得到一个Messung类,如下所示:

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() + "]";
}

}
下面是我尝试解析它的脚本部分:

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

但是,当我试图打印Messung-object时,字符串得到nullintdouble得到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]
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]

我做错了什么?

mbyulnm0

mbyulnm01#

字符大小写不匹配。在您的java类中用途:

s加拿大日期时间

而您的json文件使用:

S扫描日期时间

我会尝试更新java类或json文件中的case。

相关问题