Я пытаюсь извлечь данные из XML-файла. После преобразования XML в JSON я не могу получить доступ к данным. Я разбираю данные с помощью GSON.Gson: Ожидается BEGIN_ARRAY, но BEGIN_OBJECT
Этот фрагмент дает мне ошибку.
Любой совет? Заранее спасибо.
Type collectionType = new TypeToken<Collection<PldData.DataClass>>(){}.getType();
Collection<PldData.DataClass> enums = gson.fromJson(jsonObj.toString(),collectionType);
final PldData.DataClass [] location = enums.toArray(new PldData.DataClass[enums.size()]);
Это содержание JSON:
{
"data": {
"-ver": "12",
"Nets": {
"-ver": "13",
"Net": [
{
"-IdAliasZone": "alias",
"-FullDomain": "www.example.com",
"-PubIp": "www.example.com",
"-RemMode": "0",
"Device": [
{
"-IdAliasDevice": "alias2",
"-Description": "Hello ",
"-IdType": "asd",
"-Type": "123",
"-LocalUrl": "www.example.com"
},
{
"-IdAliasDevice": "asd",
"-Description": "desc",
"-IdType": "2",
"-Type": "6",
"-LocalUrl": "www.example.com",
"-RemotePort": "89",
"-Mac": "AA:AA:AA:AA:AA",
"-Status": "asd",
"-Version": "1",
"-Uptime": "123",
"Plants": {
"Plant1": {
"-Description": "1",
"-Enable": "1"
},
"Plant2": {
"-Description": "2",
"-Enable": "1"
},
"Plant3": {
"-Description": "3",
"-Enable": "1"
},
"Plant4": {
"-Description": "I4",
"-Enable": "1"
}
}
},
{
"-IdAliasDevice": "asd",
"-Description": "qwe",
"-IdType": "1",
"-Type": "123",
"-LocalUrl": "www.example.com"
},
{
"-IdAliasDevice": "123",
"-Description": "asd",
"-IdType": "2",
"-Type": "12",
"-LocalUrl": "www.example.com",
"-RemotePort": "12",
"-Mac": "0asd",
"Plants": {
"Plant1": {
"-Description": "1",
"-Enable": "1"
},
"Plant2": {
"-Description": "2",
"-Enable": "1"
},
"Plant3": {
"-Description": "3",
"-Enable": "1"
},
"Plant4": {
"-Description": "4",
"-Enable": "1"
}
}
},
{
"-IdAliasDevice": "asd",
"-Description": "asd-da",
"-IdType": "1",
"-Type": "254",
"-LocalUrl": "www.example.com"
},
{
"-IdAliasDevice": "223",
"-Description": "123",
"-IdType": "2",
"-Type": "64",
"-LocalUrl": "www.example.com",
"-RemotePort": "84",
"-Mac": "AA:AA:AA:AA:AA",
"-Status": "2",
"-Version": "41",
"-Uptime": "3",
"Plants": {
"Plant1": {
"-Description": " 1",
"-Enable": "1"
},
"Plant2": {
"-Description": " 2",
"-Enable": "1"
},
"Plant3": {
"-Description": " 3",
"-Enable": "1"
},
"Plant4": {
"-Description": " 4",
"-Enable": "1"
}
}
},
{
"-IdAliasDevice": "asd",
"-Description": "www.example.com",
"-IdType": "1",
"-Type": "254",
"-LocalUrl": "www.example.com"
},
{
"-IdAliasDevice": "a",
"-Description": "e",
"-IdType": "1",
"-Type": "254",
"-LocalUrl": "www.example.com"
},
{
"-IdAliasDevice": "d",
"-Description": "adsa",
"-IdType": "1",
"-Type": "254",
"-LocalUrl": "www.example.com"
},
{
"-IdAliasDevice": "123",
"-Description": "MAC",
"-IdType": "1",
"-Type": "25124",
"-LocalUrl": "www.example.com"
},
{
"-IdAliasDevice": "124",
"-Description": "WINDOWS",
"-IdType": "1",
"-Type": "2523",
"-LocalUrl": "www.example.com"
},
{
"-IdAliasDevice": "125",
"-Description": "WC",
"-IdType": "1",
"-Type": "254",
"-LocalUrl": "www.example.com",
"-RemotePort": "83"
}
]
},
{
"-IdAliasZone": "123",
"-FullDomain": "www.example.com",
"-PubIp": "www.example.com",
"-RemMode": "1",
"Device": [
{
"-IdAliasDevice": "asd",
"-Description": "desc2",
"-IdType": "21",
"-Type": "642",
"-LocalUrl": "www.example.com",
"-RemotePort": "82",
"-Mac": "AAAAAAAAA",
"Plants": {
"Plant1": {
"-Description": "1",
"-Enable": "1"
},
"Plant2": {
"-Description": "2",
"-Enable": "1"
},
"Plant3": {
"-Description": "3",
"-Enable": "1"
},
"Plant4": {
"-Description": "4",
"-Enable": "1"
}
}
}
]
}
]
}
}
}
Это следующий класс:
public class PldData {
private DataClass data;
public DataClass getData() {
return data;
}
public void setData(DataClass data) {
this.data = data;
}
public static class DataClass {
@SerializedName("-ver")
private String ver;
private NetsClass Nets;
private ObserverAreaClass ObserverArea;
public String getVer() {
return ver;
}
public void setVer(String ver) {
this.ver = ver;
}
public NetsClass getNets() {
return Nets;
}
public void setNets(NetsClass Nets) {
this.Nets = Nets;
}
public ObserverAreaClass getObserverArea() {
return ObserverArea;
}
public void setObserverArea(ObserverAreaClass ObserverArea) {
this.ObserverArea = ObserverArea;
}
public static class NetsClass {
@SerializedName("-ver")
private String ver;
private List<NetClass> Net;
public String getVer() {
return ver;
}
public void setVer(String ver) {
this.ver = ver;
}
public List<NetClass> getNet() {
return Net;
}
public void setNet(List<NetClass> Net) {
this.Net = Net;
}
public static class NetClass {
@SerializedName("-IdAliasZone")
private String IdAliasZone;
@SerializedName("-FullDomain")
private String FullDomain;
@SerializedName("-PubIp")
private String PubIp;
@SerializedName("-RemMode")
private String RemMode;
/***** with getter and setter ****/
}
}
}
}//END CLASS
Ваш JSON не является массивом, это JSON объект, но вы пытаетесь разобрать его как массив. Описание ошибки очень ясное. –
Любые советы? Я застрял Спасибо –