我正在尝试从XML文件中提取数据。将XML转换为JSON后,我无法访问数据。我正在用GSON解析数据。
此代码段给我一个错误。
任何建议?提前谢谢。
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
答案 0 :(得分:1)
你的json只出现一个对象,所以你应该使用
PldData pldData = new Gson().fromJson(jsonString, PldData.class);
更新:我尝试了一个包含资源和工作的干净项目。看截图。
答案 1 :(得分:0)
我认为问题是您的ObserverAreaClass
中有一个DataClass
,这在您的JSON架构中不存在。
从DataClass
中移除此字段,它应该可以正常工作。如果您在DataClass
中需要此课程,则应使用ExclusionStrategy
:
public @interface Exclude {}
// Excludes any field (or class) that is tagged with an "@Exclude" annotation
private static class ExcludeAnnotationExclusionStrategy implements ExclusionStrategy {
public boolean shouldSkipClass(Class<?> clazz) {
return clazz.getAnnotation(Exclude.class) != null;
}
public boolean shouldSkipField(FieldAttributes f) {
return f.getAnnotation(Exclude.class) != null;
}
}
然后您可以将此ExclusionStrategy
添加到您的GSON对象:
Gson gson = new GsonBuilder().setExclusionStrategies(new ExcludeAnnotationExclusionStrategy()).build();
您可以使用此批注对ObserverAreaClass
字段进行批注,以将其从反序列化(和序列化)中排除:
@Exclude private ObserverAreaClass observerArea;
答案 2 :(得分:0)
它就像一个魅力
JsonParser jsonParser = new JsonParser();
JsonObject idAlias = jsonParser.parse(jsonObj.toString()).getAsJsonObject();
String version = idAlias.get("ver").getAsString();
Log.d(TAG, "onSuccess: Version" + version);