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