2017-08-14 68 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每当你得到回应检查大小 –