0
votes

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
3
Your JSON is not an Array, it's a JSON Object, but you are trying to parse it like an array. Error description is very clear. - Oğuzhan Döngül
Any Advice ? I'm stuck Thank you - Posa

3 Answers

1
votes

Your json rapresent a single object so you should use

PldData pldData = new Gson().fromJson(jsonString, PldData.class);

UPDATE: I tried on a clean project with your resources and works. Look at the screenshot.

example

0
votes

I believe the issue is that you have an ObserverAreaClass in your DataClass, which does not exist in your JSON schema.

Remove this field from the DataClass and it should work. If you need this class inside your DataClass you should use an 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;
    }
}

Then you can add this ExclusionStrategy to your GSON object:

Gson gson = new GsonBuilder().setExclusionStrategies(new ExcludeAnnotationExclusionStrategy()).build();

And you can annotate the ObserverAreaClass field with this annotation to exclude it from deserialization (and serialization):

@Exclude private ObserverAreaClass observerArea;
0
votes

It works like a charm

JsonParser jsonParser = new JsonParser(); JsonObject idAlias = jsonParser.parse(jsonObj.toString()).getAsJsonObject(); String version = idAlias.get("ver").getAsString(); Log.d(TAG, "onSuccess: Version" + version);