Search code examples
javajson-lib

json String transform to Java Bean


When I run the code, it reports:

Exception in thread "main" java.lang.ClassCastException: net.sf.json.JSONObject cannot be cast to ColorData at.....

The Code is:

public class JsonTest {

public static void main(String[] args) {


    ArrayList<ColorData> list = new ArrayList<>();

    ColorData data1 = new ColorData(1129, 0.35);
    ColorData data2 = new ColorData(1120, 0.39);

    list.add(data1);
    list.add(data2);

    Collections.sort(list);

    // Java list to json
    JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(list);

    // json to Java
    JSONArray jsonObject = JSONArray.fromObject(jsonArray);
    ArrayList<ColorData> resList = new ArrayList<ColorData>();

    for (int i = 0; i < jsonObject.size(); i++) {
        ColorData data = (ColorData) jsonObject.get(i);
        resList.add(data);
    }

}
}

After the Java List to Json, it's toString is:

[{"HSV":1120,"denominator":1,"numerator":1,"quantity":0.39},{"HSV":1129,"denominator":1,"numerator":1,"quantity":0.35}]

Then read this string back to jsonObject, and parse it into ColorData, store it into the resList.

Through jsonObject.get(i) I can get the each data such as {"HSV":1120,"denominator":1,"numerator":1,"quantity":0.39} , But how can I transform it to a ColorData instance ???

The ColorData Class are as follow:

public class ColorData implements Comparable<ColorData>{

private int HSV;
private double quantity;

private int numerator = 1;
private int denominator = 1;


// init the value
public ColorData (int HSV, double quantity) {
    this.HSV = HSV;
    this.quantity = quantity;
}


// getter and setter
public int getHSV() {
    return HSV;
}
public void setHSV(int hSV) {
    HSV = hSV;
}
public double getQuantity() {
    return quantity;
}
public void setQuantity(double quantity) {
    this.quantity = quantity;
}
public int getNumerator() {
    return numerator;
}
public void setNumerator(int numerator) {
    this.numerator = numerator;
}
public int getDenominator() {
    return denominator;
}
public void setDenominator(int denominator) {
    this.denominator = denominator;
}

@Override
public int compareTo(ColorData o) {
   // TODO Auto-generated method stub
   if (this.quantity - o.quantity > 0)
    return -1;
   else if (this.quantity - o.quantity < 0)
    return 1;
   else
    return 0;
}


}

Solution

  • you need to read attributes one by one and set them in the data object.

    ColorData data;
    for (int i = 0; i < jsonObject.size(); i++) {
            data = new ColorData();
    
            //{"HSV":1120,"denominator":1,"numerator":1,"quantity":0.39}
            data.setHSV(((JSONObject)jsonObject.get(i)).getInt("HSV"));
            data.setDenominator(((JSONObject)jsonObject.get(i)).getInt("denominator"));
            data.setNumerator(((JSONObject)jsonObject.get(i)).getInt("numerator"));
            data.setQuantity(((JSONObject)jsonObject.get(i)).getDouble("quantity"));
    
            resList.add(data);
        }
    

    or you can use GSON lib.