2017-10-15 86 views
0

我得到一個json結果從服務與改造像波紋管:改裝 - 預計BEGIN_ARRAY,但是BEGIN_OBJECT?

{ 
    "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 
       } 
      } 
     ] 
    } 
} 

而且我越來越像波紋管與retrofit

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

} 
+0

您還可以添加您的ResultProducts模型類 –

+0

在此URL(http://www.jsonschema2pojo.org/)中複製上述模型並查看模型。 –

回答

1

你的問題是Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

所以更改List<ProductsModel>ProductsModel

  • 如果JSONJSONArray,您可以將其解析到List(如List<ProductsModel>)。

  • 如果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(); 
0

你改造的呼叫更改爲:

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

原因是:如果你把List<ProductsModel>它將檢查JsonArray但你的反應中它是未來JsonObject,因此只是從List<ProductsModel>改爲ProductsModel會這樣解決你的問題。

希望它有幫助!

0

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+"]"; 
    } 
} 

而且回調將是Callback<ResponseJSON>

你可以通過reponse.body()的產品結果的getResult()的getProducts()。

我從http://pojo.sodhanalibrary.com/生成了模型。你可以通過引用我給出的模型創建另一個。

0

作爲您服務的迴應,您有一個關鍵字爲「結果」的對象,包含一些字段和產品列表(數組,帶有關鍵「產品」)。你的ProductsModel是相當不錯的。但你實施改造是錯誤的,應該是:

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

的變化:所有List<ProductsModel>通過ProductsModel更換。

相關問題