2017-08-14 66 views
0

我得到一個json像咆哮,服務:驗證空花括號JSON - 安卓

[ 
    { 
     "commentID": 21, 
     "commentAuthor": "AAA", 
     "commentText": "BBB", 
     "commentDate": 1484824835, 
     "productName": "XXXXX", 
     "productUrl": "xxx.html", 
     "productPictureUrl": "yyy.jpg" 
    } , 
{ 
     "commentID": 21, 
     "commentAuthor": "AAA", 
     "commentText": "BBB", 
     "commentDate": 1484824835, 
     "productName": "XXXXX", 
     "productUrl": "xxx.html", 
     "productPictureUrl": "yyy.jpg" 
    } 
] 

我得到上面jsonretrofitGson

Problem:當json是空的像波紋管我怎麼可以驗證它?

[{}] 

我得到的json值與Gson像波紋管:

RetrofitApi.getVendorAdminApi() 
     .getComments(userToken, pageNumber) 
     .enqueue(new Callback<List<Comment>>() { 
      @Override 
      public void onResponse(Call<List<Comment>> call, Response<List<Comment>> response) { 
       if (response.isSuccessful()) { 

        resultListener.onSuccess(response.body()); 
       } else { 
        resultListener.onFailure(); 
       } 
      } 

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

爲什麼你的Web服務要返回'[{}]'而不是'[]'equals()hashcode()方法呢? –

回答

1

默認情況下,我認爲Gson omits null fields

因此,{}將是相同

{ 
    "commentID": null, 
    "commentAuthor": null, 
    "commentText": null, 
    "commentDate": null, 
    "productName": null, 
    "productUrl": null, 
    "productPictureUrl": null 
} 

其中,如果您對response.body().get(0)定義的new Comment(),並進行了比較,應該是相等的。

注:這需要你實現Comment.java

+0

我將[{}]編輯爲[]並做了很好的工作,但上面的代碼也是如此。謝謝了很多 –

0

[{} //錯誤

整頓從服務器的回覆。這將是

[] //完美

您應該檢查JSON數組SIZE

if (JsonOBJ.size()==0) 
{ 
    Toast.makeText(this, "Json Is Empty", Toast.LENGTH_LONG).show(); 
} 

編輯

if (JsonOBJ.entrySet().size()==0) 
{ 
    Toast.makeText(this, "Json Is Empty", Toast.LENGTH_LONG).show(); 
} 
+0

我從Gson使用獲取json,但我沒有JsonOBJ? –

+0

我只有JsonObject。 –

+0

@JoJoRoid每當你得到迴應檢查大小 –