I am a bit new to json and all. I wrote a program that can take in a txt file, read the header then rearrange the columns header,value into group that easier to look at.
I use javafx for GUI and json file to handle the grouping.
The program is working but i want to take it one step further. I want the color of that group change from , for example, black to red whenever the value exceed a certain threshold.
So how would i do that in json file? what is the syntax of if else ? Can you give me an example base on the snippet of the code ? Thanks
json file:
[
{
"id": 1,
"name": "Magtitue",
"columnNames": [
"magx",
"right mag",
"magy",
"forward mag",
"magz",
"down mag"
],
"color": "red"
},
{
"id": 2,
"name": "Flight Time",
"columnNames": [
"flighttime",
"flight time"
],
"color": "green"
},
{
"id": 3,
"name": "Temperature",
"columnNames": [
"temperature"
],
"color": "blue"
}
]
java handler that use json file:
@ToString
@Data
public class ColumnGroupInfo implements Comparable {
private static final Gson GSON = new Gson();
private int id;
private String name;
private String color;
private List<String> columnNames = new LinkedList<>();
public ColumnGroupInfo(int id, String name, String color) {
super();
this.id = id;
this.name = name;
this.color = color;
}
public static ColumnGroupInfo newInstance(JSONObject json) {
ColumnGroupInfo ent = GSON.fromJson(json.toString(), ColumnGroupInfo.class);
return ent;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public List<String> getColumnNames() {
return columnNames;
}
public void setColumnNames(List<String> columnNames) {
this.columnNames = columnNames;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ColumnGroupInfo)) {
return false;
}
ColumnGroupInfo columnGroupInfo = (ColumnGroupInfo) obj;
return columnGroupInfo.id == this.id &&
columnGroupInfo.name.equals(this.name) &&
columnGroupInfo.columnNames.equals(this.columnNames);
}
@Override
public String toString() {
return String.format("id:%s,name:%s,color:%s", id, name, color);
}
@Override
public int compareTo(Object o) {
if (o instanceof ColumnGroupInfo) {
ColumnGroupInfo compare = (ColumnGroupInfo) o;
return Integer.compare(getId(), compare.getId());
}
return 0;
}
}
Aucun commentaire:
Enregistrer un commentaire