如何识别从API接收的数据是JAVA中的数组格式还是对象格式?

时间:2019-04-25 12:39:41

标签: java arrays json object

这是我的第一个Java项目。

我正在使用Java中的第三方Flight API。

实际上,问题是,如果接收到的数据只有1条记录,我将获得Object格式的数据,如果接收到的数据具有1条以上记录,那么我将获得Array格式的数据。现在的问题是,我创建了一个POJO类,在其中将其定义为Array,但是当我以Object格式获取数据时,会出现错误:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1
public class MlFlightGetFlightAvailibilityResponse {

    private MlAirlineList[] AirlineList;

    public MlAirlineList[] getAirlineList() {
        return AirlineList;
    }

    public void setAirlineList(MlAirlineList[] AirlineList) {
        this.AirlineList = AirlineList;
    }

    @Override
    public String toString() {
        return "ClassPojo [AirlineList = " + AirlineList + "]";
    }
}

public class MlAirlineList {

    private String AirlineCode;
    private String AirlineName;

    public MlAirlineList(String AirlineCode, String AirlineName) {
        this.AirlineCode = AirlineCode;
        this.AirlineName = AirlineName;
    }

    public String getAirlineCode() {
        return AirlineCode;
    }

    public void setAirlineCode(String AirlineCode) {
        this.AirlineCode = AirlineCode;
    }

    public String getAirlineName() {
        return AirlineName;
    }

    public void setAirlineName(String AirlineName) {
        this.AirlineName = AirlineName;
    }

    @Override
    public String toString() {
        return "ClassPojo [AirlineCode = " + AirlineCode + ", AirlineName = " + AirlineName + "]";
    }
}

下面是我遇到错误的for循环

Map<String, String> mlFlightAirline = new HashMap<>(); // Unique Flight Airline List
Gson gson = new Gson();
MlFlightResponse mlflights = gson.fromJson(mlResponse, MlFlightResponse.class); // mlResponse is JSON response

public class MlFlightResponse {

    private MlFlightGetFlightAvailibilityResponse GetFlightAvailibilityResponse;

    public MlFlightGetFlightAvailibilityResponse getGetFlightAvailibilityResponse() {
        return GetFlightAvailibilityResponse;
    }

    public void setGetFlightAvailibilityResponse(MlFlightGetFlightAvailibilityResponse GetFlightAvailibilityResponse) {
        this.GetFlightAvailibilityResponse = GetFlightAvailibilityResponse;
    }

    @Override
    public String toString() {
        return "ClassPojo [GetFlightAvailibilityResponse = " + GetFlightAvailibilityResponse + "]";
    }
}

for (MlAirlineList airline : mlflights.getGetFlightAvailibilityResponse().getAirlineList()) {
    mlFlightAirline.put(airline.getAirlineCode(), airline.getAirlineName());
}

在上述代码中,

MlAirlineList
有时作为数组出现,有时根据可用记录数具有对象。

对象数据格式:

{
    "AirlineList": {
        "AirlineCode":"test",
        "AirlineName":"test"
    }
}

{
    "AirlineList": [{
        "AirlineCode":"test",
        "AirlineName":"test"
    },
    {
        "AirlineCode":"test",
        "AirlineName":"test"
    }]
}

请指引我正确的方向。

谢谢

2 个答案:

答案 0 :(得分:0)

据我所见,您尝试处理的JSON响应中有一个数组数组。

尝试

for (MlAirlineList airline : mlflights.getGetFlightAvailibilityResponse().getAirlineList()) {
    mlFlightAirline.put(airline[0], airline[1]);
}

答案 1 :(得分:-1)

您可以手动检查此修补程序。如果响应以“ {”开头,以“}”结尾,则可以在响应的开始和结尾部分添加[和]。这肯定可以工作