使用没有键的 ArrayList 的 ArrayList 转换/反序列化 JSON

问题描述

我有一个包含多个数组的 json 文件。当这不是给定的时,我不清楚如何设置键值对。我知道 a [ … ] 代表一个数组,而 { … } 代表一个对象。我查看了在线教程,但没有人回答我的问题以及我在此 JSON 中找到的内容

问题: 我正在尝试将 JSON 中的每个属性转换为 POJO/dataTypes。下面的数组列表中有这些没有键值对的属性。 "5","1964-07-20",null,"7",在这个数组列表中有具有键值对的属性。 { "retail": "7.05","price": "12.07","cost": "3.01","fees": "1.75","profit": "5.85" } 如何创建一个同时处理和的类将每个属性存储到一个数据类型。 它将在开始时具有相同的字段(5 个无键属性),它将始终相同。价值只是改变。

提前致谢。

没有与以下属性关联的键值

  "5",

以下是我尝试创建的两个类和原始 JSON 文件。也是一个自动生成的 JSON。

音乐.java

import java.util.ArrayList;

public class Music {
    private String artist;
    private String record;
    ArrayList<MusicRecords> musicRecords = new ArrayList<MusicRecords>();


    public String getArtist() {
        return artist;
    }

    public String getRecord() {
        return record;
    }

    public ArrayList<MusicRecords> getMusicRecords() {
        return musicRecords;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public void setRecord(String record) {
        this.record = record;
    }

    public void setMusicRecords(ArrayList<MusicRecords> musicRecords) {
        this.musicRecords = musicRecords;
    }
}

MusicRecords.java

public class MusicRecords {

    private double retail;
    private double price;
    private double cost;
    private double fees;
    private double profit;


    public double getRetail() {
        return retail;
    }

    public void setRetail(double retail) {
        this.retail = retail;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public double getFees() {
        return fees;
    }

    public void setFees(double fees) {
        this.fees = fees;
    }

    public double getProfit() {
        return profit;
    }

    public void setProfit(double profit) {
        this.profit = profit;
    }
}

音乐.json

{
  "artist": "Beatles","record": "Something New","musicRecords": [
    [
      "5",{
        "retail": "7.05","profit": "5.85"
      },{
        "retail": "8.05","cost": "5.01","fees": "3.75","profit": "7.85"
      },{
        "retail": "5.05","price": "10.07","cost": "2.01","profit": "4.85"
      },{
        "retail": "9.05","price": "14.07","profit": "7.85"
      }
    ],[
      null,"1967-05-17","4",2,"profit": "7.85"
      }
    ]
  ]
}

下面是自动生成的 JSON 格式化程序。它不会为下面显示的内部数组创建键值对。它创建一个 Object 类型的 ArrayList。

        "retail": "8.05","profit": "7.85"

JSON 自动格式化文件

public class Application {
  private String artist;
  private String record;
  ArrayList<Object> musicRecords = new ArrayList<Object>();


 // Getter Methods 

  public String getArtist() {
    return artist;
  }

  public String getRecord() {
    return record;
  }

 // Setter Methods 

  public void setArtist( String artist ) {
    this.artist = artist;
  }

  public void setRecord( String record ) {
    this.record = record;
  }
}

最后,这些是我访问过但找不到答案的网站。

https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java
https://stackoverflow.com/questions/37816746/reading-a-json-file-in-java
http://theoryapp.com/parse-json-in-java/
https://mkyong.com/java/how-to-parse-json-with-gson/
http://tutorials.jenkov.com/java-json/gson.html#parsing-json-into-java-objects

I also used these auto generators below:
https://www.freecodeformat.com/json2pojo.PHP
http://pojo.sodhanalibrary.com/
http://www.jsonschema2pojo.org/
https://www.site24x7.com/tools/json-to-java.html

解决方法

这里的主要问题似乎在于您的 JSON 格式。它以 artistrecord 等开头,但随后您定义了一个 musicRecords 数组,它似乎不仅表示一组记录,还表示一些额外的数据(例如日期 "1964-07-20")。

在不知道此结构的用途的情况下,这些字段似乎属于根 JSON 对象中的 artistrecord 字段,它们需要某种键来表示其目的。

然后,您可以轻松制作一个更具逻辑意义且更易于在您的项目中使用的 POJO。


但是,如果 JSON 文件来自您无法影响的外部来源,您有两种选择:

使用您发布的 JSON 自动格式化文件

这种方法可能不是最好的主意,主要是因为 POJO 不代表易于在代码中使用的逻辑对象,而且还因为类型安全。您必须将列表中的所有对象向上转换为 Object,这意味着在需要访问它们时再次将它们全部向下转换。这通常是不好的做法,如果 JSON 结构完全改变,这将很难调试。

使用不同的 JSON 库

最好的解决方案是使用 JSON 库,该库允许您逐步读取 JSON 文件,然后映射到您熟悉并能更好地代表您的用例的 POJO。但是,据我所知,GSON 是一个直接反序列化库,因此无法提供映射到不同格式所需的微调。