当前位置: 首页 > 知识库问答 >
问题:

期待BEGIN_ARRAY但BEGIN_OBJECT

呼延俊良
2023-03-14

我正在尝试从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

共有3个答案

轩辕煌
2023-03-14

它就像一个咒语

JsonParser jsonParser=new JsonParser(); JsonObject idAlias=jsonParser.parse(jsonObj.toString()). getAsJsonObject(); String version=idAlias.get("ver"). getAsString(); Logo. d(TAG,"on成功:版本"version);

濮阳钟展
2023-03-14

我认为问题在于,您的数据类中有一个ObserverAreaClass,而JSON模式中不存在该类。

数据类中删除此字段,它应该可以工作。如果在数据类中需要此类,则应使用排除策略:

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;
    }
}

然后您可以将此Exdu sionStrategy添加到您的GSON对象:

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

您可以使用此注释注释ObserverArea aClass字段以将其从反序列化(和序列化)中排除:

@Exclude private ObserverAreaClass observerArea;
吕俊才
2023-03-14

json表示单个对象,因此应该使用

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

更新:我用你的资源和作品尝试了一个干净的项目。看截图。

 类似资料:
  • 我正在学习改装2,遇到如下错误: 带系统。错误:com。谷歌。格森。JsonSyntaxException:java。lang.IllegalStateException:应为BEGIN\u对象,但在第1行第94列路径$处为BEGIN\u数组。com的riceField。谷歌。格森。内部的绑定ReflectiveTypeAdapterFactory$适配器。阅读(ReflectiveTypeAda

  • 我的Retrofit2调用出现以下异常: 学分类别:

  • 我正在通过改型和RXAndroid与来自android的REST API进行交互,我遇到了一个问题。 当我向我的“api/users/{id}/{id}/classes”发送GET请求时,我的api会以用户类列表作出响应,如下所示: 我的界面如下所示: 我从片段中这样称呼它: 我的类对象以及getter和setter如下所示: gson不应该看到“类”:[]数组并从中解析对象吗?我做错了什么

  • 问题内容: 我在列表中仅收到一个项目时收到此错误。我在服务器端REST Web服务中使用Jersey,当List返回一个元素并且当我得到它时,我仅收到错误,但是当它包含多个元素时,它可以正常工作,这就是我的工作方式它: 为什么Jersey不发送包含一个项目的列表?这是球衣的虫子吗?有没有办法使它始终序列化为数组? 当Web服务返回0元素时,这是我的JSON数据: 当Web服务返回1元素时,这是我的

  • 我试图使用gson解析下面的json字符串,并且我得到了这个异常。 异常很明显,它期待一个对象(可能是Datapoint),但它遇到了一个数组。我确信是我的模型类导致了这个问题,但是我不明白我的模型类应该是什么样子才能解析json而不会失败。

  • 我正在使用Gson库保存和检索玩家对象的ArrayList。 但当运行此活动时,我会得到以下错误日志: 原因:java。lang.IllegalStateException:应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象$ 这段代码怎么了?