简体   繁体   中英

Converting/Deserializing a JSON with an ArrayList of ArrayList without a Key

I have a json file with multiple arrays. It is not clear to me how to set up Key-Value pairs when this isn't one given. I understand that a [ … ] represents an array and { … } represents an object. I looked at tutorials online but none had an answer to my question and what I found in this JSON.

The Problem: I'm trying to convert every property in the JSON to POJO / dataTypes. There are these properties in the arraylist below that don't have a key-value pair. "5", "1964-07-20", null, "7", null, Within this arraylist there are properties that have a key-value pair. { "retail": "7.05", "price": "12.07", "cost": "3.01", "fees": "1.75", "profit": "5.85" } How to create a class that handles both and stores each property to a datatype. It will have the same fields at the start (the 5 none key properties), It will always be the same. The Value is what only changes.

Thanks in advance.

There is no Key-Value associated with the below properties

  "5",
  "1964-07-20",
  null,
  "7",
  null,

Below are my two classes that I attempted to create and the original JSON file. Also an auto-generated JSON.

Music.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;
    }
}

Music.json

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

Below is the auto-generated JSON Formatter. It doesn't create key-value pairs for the inner arrays shown below. It creates an ArrayList of type Object.

        "retail": "8.05",
        "price": "12.07",
        "cost": "5.01",
        "fees": "3.75",
        "profit": "7.85"

JSON Auto Formatted File

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

Finally, these are the websites that I visited to find an answer but couldn't.

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

The main problem here seems to lie with your JSON format. It starts off fine with the artist , record etc. but then you have defined an array of musicRecords which seems to not only represent an array of records but also some extra data as well (a date "1964-07-20" for example).

Without knowing what this structure is for, it would seem like those fields belong with the artist and record fields in your root JSON object, where they need some sort of key to represent their purpose.

Then you could easily make a POJO that makes a bit more logical sense and will be easier to use in your project.


However if the JSON file is coming from an external source that you have no influence over, you have two options:

Use the JSON Auto Formatted File that you posted

This approach is probably not the best idea, mostly due to the POJO then not representing a logical object that will be easy to use in your code but also because of type safety. You have to upcast all of the objects in your list to Object which would mean casting them all back down again when they need to be accessed. This is generally bad practice and if the JSON structure changed at all this would be very difficult to debug.

Use a different JSON library

The best solution would be to use a JSON library that allows you to read a JSON file step by step and then map to a POJO that you're comfortable with and better represents your use case. However as far as I am aware, GSON is a direct deserialization library so cannot offer the fine tuning you would need to map to a different format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM