I'm trying to extract data from an XML file. After converting XML to JSON, I'm not able to access to data. I'm parsing data with GSON.
This snippet gives me an error.
Any Advice? Thanks in advance.
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()]);
This is the JSON content:
{
"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"
}
}
}
]
}
]
}
}
}
This is the following class:
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
