简体   繁体   中英

Parse json array with different objects to their classes

I have the following json:

[
    {
        "type": 1,
        "Steps": {
            "steps": steps
        }
    },
    {
        "type": 2,
        "HeartRate": {
            "heartrates": heartRates
        }
    }
]

The field steps is just an int an heartRates is an array of int's. I want to parse this using gson to two classes. I found a similar question on stackoverflow: How parse json array with multiple objects by gson? and tried it but it didn't work. This is my code:

public class DataModels extends ArrayList<DataModels.Container> {

public class Container {
    public int type;
    public Object object;
}

public class Steps {

    double steps;

    public Steps(double steps) {
        this.steps = steps;
    }

    public double getSteps() {
        return steps;
    }

    @Override
    public String toString() {
        return "Steps: " + steps;
    }

}

public class HeartRate {

    int heartRate;

    public HeartRate(int hr) {
        heartRate = hr;
    }

    public double getHeartRate() {
        return heartRate;
    }

    @Override
    public String toString() {
        return "Heart rate: " + heartRate;
    }

}
}

Then for parsing my json:

public String getJSONMessage(String gearSData) {
    System.out.println(gearSData);
    Gson gson = new Gson();
    DataModels model = gson.fromJson(gearSData, DataModels.class);
    System.out.println(model);
    for (DataModels.Container container: model) {

        System.out.println(container.type);
        System.out.println(container.object);
        String innerJson = gson.toJson(container.object);
        System.out.println("InnerJson: " + innerJson);

        switch (container.type) {
            case 1:
                DataModels.Steps steps = gson.fromJson(innerJson, DataModels.Steps.class);
                System.out.println(steps);
                break;
            case 2:
                DataModels.HeartRate heartRate = gson.fromJson(innerJson, DataModels.HeartRate.class);
                System.out.println(heartRate);
                break;
        }
    }
}

The type gets parsed correctly but the innerjson is null and I don't know why. Can somebody explain this or does someone know a better way to do this?

Names of your fields should be equal to the fields in json. I just renamed fields of your classes and your code works well for me:

public static String getJSONMessage(String gearSData) {
    System.out.println(gearSData);
    Gson gson = new Gson();
    DataModels model = gson.fromJson(gearSData, DataModels.class);
    System.out.println(model);
    for (DataModels.Container container : model) {

        System.out.println(container.type);
        String innerJson = gson.toJson(container.type == 1 ? container.Steps : container.HeartRate);
        System.out.println("InnerJson: " + innerJson);
        //...
    }
    return null;
}

public static class DataModels extends ArrayList<DataModels.Container> {
    public static class Container {
        public int type;
        public StepsType Steps; // object for type 1
        public HeartRateType HeartRate; // object for type 2
    }

    public static class StepsType {
        double steps;
        //...
    }

    public static class HeartRateType {
        int heartrates;
        //...
    }
}

What I did is to create a base class that would only have the header of the message. Then created several classes that inherited from that base class that would have specific data.

To decode would be a two steps process, use the base class to decode and get the header of the message. One of the fields in the header would be an identifier that would define which sub-class should be used to de-code (in your case that would be the "type"). put the type in a Switch and use the proper sub-class to decode the entire message.

It would look something like this:

class Base {
    public int type;
}

class Sub1 extends Base {
    public String myDataString;
}

class Sub2 extends Base {
    public int myDataInt;
}

Then to de-code

Base theBase = theGsonData.fromJson(theJsonString, Base.class);

Switch (theBase.type) {
case 1: Sub1 theSub1 = theGsonData.fromJson(theJsonString, Sub1.class); break;
case 2: Sub2 theSub2 = theGsonData.fromJson(theJsonString, Sub2.class); break;
}

Hope this helps.

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