Java解析的JSON数组(使用GSON),返回零或null。

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

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:

[
  {
    &quot;ScanDateTime&quot;: &quot;2020-10-11T00:04:05&quot;,
    &quot;RecipeName&quot;: &quot;_BLANK_16&quot;,
    &quot;Slot&quot;: 13,
    &quot;DefectTotal&quot;: 0,
    &quot;AreaCount&quot;: 3,
    &quot;Part1&quot;: 0,
    &quot;Part2&quot;: 0,
    &quot;Part3&quot;: 0,
    &quot;Part4&quot;: 0,
    &quot;Part5&quot;: 0,
    &quot;Part6&quot;: 0,
    &quot;Part7&quot;: 0,
    &quot;Part8&quot;: 0,
    &quot;HazeRegion&quot;: 0,
    &quot;HazeAverage&quot;: 0,
    &quot;HazePeak&quot;: 0,
    &quot;TotalScratch&quot;: 0
  },
  {
    &quot;ScanDateTime&quot;: &quot;2020-10-11T00:34:36&quot;,
    &quot;RecipeName&quot;: &quot;_BLANK_16&quot;,
    &quot;Slot&quot;: 24,
    &quot;DefectTotal&quot;: 50,
    &quot;AreaCount&quot;: 5,
    &quot;Part1&quot;: 21,
    &quot;Part2&quot;: 25,
    &quot;Part3&quot;: 0,
    &quot;Part4&quot;: 1,
    &quot;Part5&quot;: 1,
    &quot;Part6&quot;: 2,
    &quot;Part7&quot;: 0,
    &quot;Part8&quot;: 0,
    &quot;HazeRegion&quot;: 0,
    &quot;HazeAverage&quot;: 0,
    &quot;HazePeak&quot;: 0,
    &quot;TotalScratch&quot;: 0
  },
  {
    &quot;ScanDateTime&quot;: &quot;2020-10-11T00:35:26&quot;,
    &quot;RecipeName&quot;: &quot;_BLANK_16&quot;,
    &quot;Slot&quot;: 23,
    &quot;DefectTotal&quot;: 192,
    &quot;AreaCount&quot;: 2401,
    &quot;Part1&quot;: 34,
    &quot;Part2&quot;: 63,
    &quot;Part3&quot;: 30,
    &quot;Part4&quot;: 16,
    &quot;Part5&quot;: 9,
    &quot;Part6&quot;: 40,
    &quot;Part7&quot;: 0,
    &quot;Part8&quot;: 0,
    &quot;HazeRegion&quot;: 0,
    &quot;HazeAverage&quot;: 0,
    &quot;HazePeak&quot;: 0,
    &quot;TotalScratch&quot;: 39.205788
  },
  {
    &quot;ScanDateTime&quot;: &quot;2020-10-11T03:05:29&quot;,
    &quot;RecipeName&quot;: &quot;_BLANK_16&quot;,
    &quot;Slot&quot;: 0,
    &quot;DefectTotal&quot;: 945,
    &quot;AreaCount&quot;: 57,
    &quot;Part1&quot;: 273,
    &quot;Part2&quot;: 479,
    &quot;Part3&quot;: 93,
    &quot;Part4&quot;: 33,
    &quot;Part5&quot;: 35,
    &quot;Part6&quot;: 32,
    &quot;Part7&quot;: 0,
    &quot;Part8&quot;: 0,
    &quot;HazeRegion&quot;: 0,
    &quot;HazeAverage&quot;: 0,
    &quot;HazePeak&quot;: 0,
    &quot;TotalScratch&quot;: 0
  },
  {
    &quot;ScanDateTime&quot;: &quot;2020-10-11T03:06:55&quot;,
    &quot;RecipeName&quot;: &quot;_BLANK_16&quot;,
    &quot;Slot&quot;: 4,
    &quot;DefectTotal&quot;: 842,
    &quot;AreaCount&quot;: 78,
    &quot;Part1&quot;: 203,
    &quot;Part2&quot;: 469,
    &quot;Part3&quot;: 95,
    &quot;Part4&quot;: 33,
    &quot;Part5&quot;: 13,
    &quot;Part6&quot;: 29,
    &quot;Part7&quot;: 0,
    &quot;Part8&quot;: 0,
    &quot;HazeRegion&quot;: 0,
    &quot;HazeAverage&quot;: 0,
    &quot;HazePeak&quot;: 0,
    &quot;TotalScratch&quot;: 0
  },
  {
    &quot;ScanDateTime&quot;: &quot;2020-10-11T03:07:44&quot;,
    &quot;RecipeName&quot;: &quot;_BLANK_16&quot;,
    &quot;Slot&quot;: 9,
    &quot;DefectTotal&quot;: 391,
    &quot;AreaCount&quot;: 56,
    &quot;Part1&quot;: 125,
    &quot;Part2&quot;: 214,
    &quot;Part3&quot;: 25,
    &quot;Part4&quot;: 6,
    &quot;Part5&quot;: 11,
    &quot;Part6&quot;: 10,
    &quot;Part7&quot;: 0,
    &quot;Part8&quot;: 0,
    &quot;HazeRegion&quot;: 0,
    &quot;HazeAverage&quot;: 0,
    &quot;HazePeak&quot;: 0,
    &quot;TotalScratch&quot;: 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 &quot;Messung [ScanDateTime=&quot; + scanDateTime + &quot;, RecipeName=&quot; + recipeName + &quot;, Slot=&quot; + slot + &quot;, DefectTotal=&quot; + defectTotal + &quot;, AreaCount=&quot; + areaCount + &quot;, Part1=&quot; + part1 + &quot;, Part2=&quot; + part2 + &quot;, Part3=&quot; + part3 + &quot;, Part4=&quot; + part4 + &quot;, Part5=&quot; + part5 + &quot;, Part6=&quot; + part6 + &quot;, Part7=&quot; + part7 + &quot;, Part8=&quot; + part8 + &quot;, HazeRegion=&quot; + hazeRegion + &quot;, HazePeak=&quot; + hazePeak + &quot;, TotalScratch=&quot; + getTotalScratch() + &quot;]&quot;;
}

}

This is the part of my script where I try to parse it:

while(!isExisting){
        try {
            File myObj = new File(&quot;C:\\workspace\\AccessDBReader-6420-II\\AccessDBReader-6420-II\\bin\\Release\\Publish\\Messung-6420-II.txt&quot;);
            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.

huangapple
  • 本文由 发表于 2020年10月13日 19:06:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64334040.html
匿名

发表评论

匿名网友

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

确定