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

改装-应为BEGIN\u数组,但是否为BEGIN\u对象?

濮阳靖
2023-03-14

我从以下改装服务中获得了一个json结果:

{
    "result": {
        "totalCount": 15,
        "resultCount": 2,
        "offset": 0,
        "limit": 2,
        "products": [
            {
                "id": 10081,
                "name": "prod",
                "pictureUrl": "url",
                "price": 1,
                "url": "url",
                "briefDescription": "test",
                "description": "test",
                "pictures": [],
                "categoryTitle": "s s",
                "categoryId": 53,
                "extraInfo": {
                    "productProperties": [
                        {
                            "id": 88,
                            "value": "6",
                            "measurementUnit": "s",
                            "title": "s"
                        },
                        {
                            "id": 89,
                            "value": "2",
                            "measurementUnit": "s",
                            "title": "s s"
                        },
                        {
                            "id": 90,
                            "value": "2",
                            "measurementUnit": "s",
                            "title": "s s s s"
                        },
                        {
                            "id": 91,
                            "value": "",
                            "measurementUnit": "",
                            "title": "s s"
                        },
                        {
                            "id": 92,
                            "value": "",
                            "measurementUnit": "",
                            "title": "s s"
                        },
                        {
                            "id": 93,
                            "value": "",
                            "measurementUnit": "",
                            "title": "s"
                        },
                        {
                            "id": 94,
                            "value": "",
                            "measurementUnit": "",
                            "title": "s"
                        }
                    ],
                    "published": false,
                    "preparationTime": 1,
                    "keywords": "",
                    "quantity": 0,
                    "status": 1
                }
            },
            {
                "id": 51,
                "name": "nam3",
                "pictureUrl": "url",
                "price": 495000,
                "url": "url",
                "briefDescription": "sdsds",
                "description": "-",
                "pictures": [],
                "categoryTitle": "x  x x",
                "categoryId": 179,
                "extraInfo": {
                    "productProperties": [
                        {
                            "id": 67,
                            "value": "1000",
                            "measurementUnit": "x",
                            "title": "x x"
                        },
                        {
                            "id": 68,
                            "value": "1050",
                            "measurementUnit": "s",
                            "title": "x x x"
                        },
                        {
                            "id": 69,
                            "value": "",
                            "measurementUnit": "",
                            "title": "x x"
                        },
                        {
                            "id": 70,
                            "value": "",
                            "measurementUnit": "",
                            "title": "x x"
                        },
                        {
                            "id": 71,
                            "value": "",
                            "measurementUnit": "",
                            "title": "xxxx"
                        }
                    ],
                    "published": true,
                    "preparationTime": 2,
                    "keywords": "Aswddfe",
                    "quantity": 93,
                    "status": 1
                }
            }
        ]
    }
} 

我在改装后变得像bellow一样:

RetrofitApi.getVendorAdminApi()
        .getAdminProductss(userToken, limit, pageNumber, filters)
        .enqueue(new Callback<List<ProductsModel>>() {
            @Override
            public void onResponse(Call<List<ProductsModel>> call, Response<List<ProductsModel>> response) {
                if (response.isSuccessful()) {
                    resultListener.onSuccess(response.body());
                } else {
                    resultListener.onFailure();
                }
            }

            @Override
            public void onFailure(Call<List<ProductsModel>> call, Throwable t) {
                resultListener.onFailure();
                t.printStackTrace();
            }
        });

但是告诉我:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

下面是我的模型:

public class ProductsModel {

    @SerializedName("result")
    @Expose
    private ResultProducts result;

    public ResultProducts getResult() {
        return result;
    }

    public void setResult(ResultProducts result) {
        this.result = result;
    }

}

共有3个答案

吕淮晨
2023-03-14

json数据中的“result”节点是一个对象而不是数组。json的模型类如下

public class ResponseJSON
{
    private Result result;

    public Result getResult ()
    {
        return result;
    }

    public void setResult (Result result)
    {
        this.result = result;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [result = "+result+"]";
    }
}

public class Result
{
    private String limit;

    private String totalCount;

    private String resultCount;

    private String offset;

    private Products[] products;

    public String getLimit ()
    {
        return limit;
    }

    public void setLimit (String limit)
    {
        this.limit = limit;
    }

    public String getTotalCount ()
    {
        return totalCount;
    }

    public void setTotalCount (String totalCount)
    {
        this.totalCount = totalCount;
    }

    public String getResultCount ()
    {
        return resultCount;
    }

    public void setResultCount (String resultCount)
    {
        this.resultCount = resultCount;
    }

    public String getOffset ()
    {
        return offset;
    }

    public void setOffset (String offset)
    {
        this.offset = offset;
    }

    public Products[] getProducts ()
    {
        return products;
    }

    public void setProducts (Products[] products)
    {
        this.products = products;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [limit = "+limit+", totalCount = "+totalCount+", resultCount = "+resultCount+", offset = "+offset+", products = "+products+"]";
    }
}

public class ProductProperties
{
    private String id;

    private String title;

    private String measurementUnit;

    private String value;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getTitle ()
    {
        return title;
    }

    public void setTitle (String title)
    {
        this.title = title;
    }

    public String getMeasurementUnit ()
    {
        return measurementUnit;
    }

    public void setMeasurementUnit (String measurementUnit)
    {
        this.measurementUnit = measurementUnit;
    }

    public String getValue ()
    {
        return value;
    }

    public void setValue (String value)
    {
        this.value = value;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", title = "+title+", measurementUnit = "+measurementUnit+", value = "+value+"]";
    }
}

public class Products
{
    private String id;

    private String price;

    private String categoryTitle;

    private String briefDescription;

    private String pictureUrl;

    private String description;

    private String categoryId;

    private String name;

    private ExtraInfo extraInfo;

    private String[] pictures;

    private String url;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getPrice ()
    {
        return price;
    }

    public void setPrice (String price)
    {
        this.price = price;
    }

    public String getCategoryTitle ()
    {
        return categoryTitle;
    }

    public void setCategoryTitle (String categoryTitle)
    {
        this.categoryTitle = categoryTitle;
    }

    public String getBriefDescription ()
    {
        return briefDescription;
    }

    public void setBriefDescription (String briefDescription)
    {
        this.briefDescription = briefDescription;
    }

    public String getPictureUrl ()
    {
        return pictureUrl;
    }

    public void setPictureUrl (String pictureUrl)
    {
        this.pictureUrl = pictureUrl;
    }

    public String getDescription ()
    {
        return description;
    }

    public void setDescription (String description)
    {
        this.description = description;
    }

    public String getCategoryId ()
    {
        return categoryId;
    }

    public void setCategoryId (String categoryId)
    {
        this.categoryId = categoryId;
    }

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    public ExtraInfo getExtraInfo ()
    {
        return extraInfo;
    }

    public void setExtraInfo (ExtraInfo extraInfo)
    {
        this.extraInfo = extraInfo;
    }

    public String[] getPictures ()
    {
        return pictures;
    }

    public void setPictures (String[] pictures)
    {
        this.pictures = pictures;
    }

    public String getUrl ()
    {
        return url;
    }

    public void setUrl (String url)
    {
        this.url = url;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", price = "+price+", categoryTitle = "+categoryTitle+", briefDescription = "+briefDescription+", pictureUrl = "+pictureUrl+", description = "+description+", categoryId = "+categoryId+", name = "+name+", extraInfo = "+extraInfo+", pictures = "+pictures+", url = "+url+"]";
    }
}

public class ExtraInfo
{
    private String keywords;

    private ProductProperties[] productProperties;

    private String preparationTime;

    private String status;

    private String quantity;

    private String published;

    public String getKeywords ()
    {
        return keywords;
    }

    public void setKeywords (String keywords)
    {
        this.keywords = keywords;
    }

    public ProductProperties[] getProductProperties ()
    {
        return productProperties;
    }

    public void setProductProperties (ProductProperties[] productProperties)
    {
        this.productProperties = productProperties;
    }

    public String getPreparationTime ()
    {
        return preparationTime;
    }

    public void setPreparationTime (String preparationTime)
    {
        this.preparationTime = preparationTime;
    }

    public String getStatus ()
    {
        return status;
    }

    public void setStatus (String status)
    {
        this.status = status;
    }

    public String getQuantity ()
    {
        return quantity;
    }

    public void setQuantity (String quantity)
    {
        this.quantity = quantity;
    }

    public String getPublished ()
    {
        return published;
    }

    public void setPublished (String published)
    {
        this.published = published;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [keywords = "+keywords+", productProperties = "+productProperties+", preparationTime = "+preparationTime+", status = "+status+", quantity = "+quantity+", published = "+published+"]";
    }
}

public class ProductProperties
{
    private String id;

    private String title;

    private String measurementUnit;

    private String value;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getTitle ()
    {
        return title;
    }

    public void setTitle (String title)
    {
        this.title = title;
    }

    public String getMeasurementUnit ()
    {
        return measurementUnit;
    }

    public void setMeasurementUnit (String measurementUnit)
    {
        this.measurementUnit = measurementUnit;
    }

    public String getValue ()
    {
        return value;
    }

    public void setValue (String value)
    {
        this.value = value;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", title = "+title+", measurementUnit = "+measurementUnit+", value = "+value+"]";
    }
}

并且回调将是回调

您可以通过响应获得产品结果。正文()。getResult()。getProducts();

我已根据http://pojo.sodhanalibrary.com/.如果需要,您可以参考我给出的模型创建另一个。

司徒胤
2023-03-14

将改装呼叫更改为:

RetrofitApi.getVendorAdminApi()
        .getAdminProductss(userToken, limit, pageNumber, filters)
        .enqueue(new Callback<ProductsModel>() {
            @Override
            public void onResponse(Call<ProductsModel> call, Response<ProductsModel> response) {
                if (response.isSuccessful()) {
                    resultListener.onSuccess(response.body());
                } else {
                    resultListener.onFailure();
                }
            }

            @Override
            public void onFailure(Call<ProductsModel> call, Throwable t) {
                resultListener.onFailure();
                t.printStackTrace();
            }
        });

原因是:如果您将

希望有帮助!

韦澄邈
2023-03-14

您的问题是预期BEGIN_ARRAY但BEGIN_OBJECT在第1行第2列路径$

所以改变列表

>

  • 如果JSONJSONArray,您可以将其解析为List(如List

    如果JSONJSONObject,您可以将其解析为Object(如ProductsModel)。

    更改为此。

    @Override
    public void onResponse(Call<ProductsModel> call, Response<ProductsModel> response) {
        if (response.isSuccessful()) {
            resultListener.onSuccess(response.body());
        } else {
            resultListener.onFailure();
        }
    }
    

    而且

    Call<ProductsModel> getAdminProductss();
    

  •  类似资料:
    • 这是我的JSON数据: 这是我的反序列化代码: 我在PageSecurity上收到错误。这是我的PageResponce类。我想我可能处理信息是正确的。我只使用过JsonObject。我想知道如何获取“page”: 0,然后如何获取“线程” } 我的页面类 公共类页面{ }

    • 我正在尝试使用《纽约时报》API,并使用Observable进行改装。但我在尝试使用数据时遇到了这个错误。 有人能帮我看看哪里错了吗? 以下是我的ApiServices界面: 这是我的ApiStreams课 这就是我在我的主要活动中要做的。现在我只想在文本视图中显示每个标题的列表。。。 [编辑]TopStories和NewsItem有我的两个模型 上部结构: 新闻项目: 以下是JSON的外观: J

    • 我有下面的JSON和模型类,如何使用GSOn反序列化这个JSON,下面的代码片段会出现类似“预期的BEGIN\u对象,但是BEGIN\u数组”的异常。如何为下面的JSON数据生成模型CALS

    • 我有这样的json。在我的情况下,当我单击一个产品时,我会将该产品交给detailproduct。在那之后,我在我的详细信息屏幕中有了回收器。我想按产品数组获取回收商项目,该数组的id等于单击的产品id。 我想了解产品阵列。我的api方法正在返回调用

    • 从Android改型读取JSON字符串时出现问题。

    • 我有如下JSON文件http://sawbo-illinois.org/mobileApp.php . 我已将对象创建为: 但是当我像这样跟着的时候,我得到了改造失败的回应。我的问题在哪里? 我的完整代码是: 改造界面: 可能回调和转换过程: