使用GSON将JSON解析为Object,与列表或对象无关

时间:2014-11-01 18:05:32

标签: android json gson

我正在从Rejseplanen.dk实现API,我可以在其中接收某个或某个对象的列表。问题是我不知道什么时候会收到什么。以下是我可以收到的一个小例子:

这里的示例我将收到一个列表:(完整请求:

    "JourneyName": [
        {
            "name": "IC 865",
            "routeIdxFrom": "0",
            "routeIdxTo": "29"
        },
        {
            "name": "IC 865",
            "routeIdxFrom": "29",
            "routeIdxTo": "37"
        }
    ],
    "JourneyType": [
        {
            "type": "IC",
            "routeIdxFrom": "0",
            "routeIdxTo": "29"
        },
        {
            "type": "IC",
            "routeIdxFrom": "29",
            "routeIdxTo": "37"
        }
    ],

对象示例:

    "JourneyName": {
        "name": "Bybus 5",
        "routeIdxFrom": "0",
        "routeIdxTo": "41"
    },
    "JourneyType": {
        "type": "TB",
        "routeIdxFrom": "0",
        "routeIdxTo": "41"
    },

我正在使用GSON将收到的JSON字符串解析为JAVA对象,如下所示:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
MyClass myClass = gson.fromJson(getResponse(), MyClass.class);

我如何才能进行此转换?我认为我可以简单地将对象视为列表,但如果我这样做,我将得到这个例外:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 258 column 18 path $.JourneyDetail.JourneyName

1 个答案:

答案 0 :(得分:1)

撰写自定义TypeAdapter

public class Journey {
    private JourneyName[] journeyNameArr;
    private JourneyType[] journeyTypeArr;   
}

public class JourneyName {
    private String name, routeIdxFrom, routeIdxTo;
}

public class JourneyType {
    private String type, routeIdxFrom, routeIdxTo;
}

public class JourneyTypeAdapter extends TypeAdapter<Journey> {

    @Override
    public Journey read(JsonReader in) throws IOException {
        Journey journey= new Journey();

        Gson gson = new Gson();

        JsonParser jsonParser = new JsonParser();
        JsonObject jo = (JsonObject)jsonParser.parse(in);
        JsonElement jeJourneyName = jo.get("JourneyName");
        if(jeJourneyName instanceof JsonArray) {
            journey.journeyNameArr = gson.fromJson(jeJourneyName, JourneyName[].class);
        } else if(jeJourneyName instanceof JsonObject) {
            journey.journeyNameArr = new JourneyName[1];
            journey.journeyNameArr[0] = gson.fromJson(jeJourneyName, JourneyName.class);
        }

        JsonElement jeJourneyType = jo.get("JourneyType");
        if(jeJourneyType instanceof JsonArray) {
            journey.journeyTypeArr = gson.fromJson(jeJourneyType, JourneyType[].class);
        } else if(jeJourneyType instanceof JsonObject) {
            journey.journeyTypeArr = new JourneyType[1];
            journey.journeyTypeArr[0] = gson.fromJson(jeJourneyType, JourneyType.class);
        }
        return journey;
    }

    @Override
    public void write(JsonWriter out, Journey item) throws IOException {
        throw new IllegalArgumentException("JourneyTypeAdapter.write method not implemented!");
    }
}

测试:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Journey.class, new JourneyTypeAdapter());
Gson gson = gsonBuilder.create();

String jsonTest1 = "{\"JourneyName\":[{\"name\":\"IC 865\",\"routeIdxFrom\":\"0\",\"routeIdxTo\":\"29\"},{\"name\":\"IC 865\",\"routeIdxFrom\":\"29\",\"routeIdxTo\":\"37\"}],\"JourneyType\":[{\"type\":\"IC\",\"routeIdxFrom\":\"0\",\"routeIdxTo\":\"29\"},{\"type\":\"IC\",\"routeIdxFrom\":\"29\",\"routeIdxTo\":\"37\"}]}";
String jsonTest2 = "{\"JourneyName\":{\"name\":\"Bybus 5\",\"routeIdxFrom\":\"0\",\"routeIdxTo\":\"41\"},\"JourneyType\":{\"type\":\"TB\",\"routeIdxFrom\":\"0\",\"routeIdxTo\":\"41\"}}";

Journey journey0 = gson.fromJson(jsonTest1, Journey.class);
Journey journey1 = gson.fromJson(jsonTest2, Journey.class);