验证 null 大括号 json - Android

Validation null Curly brackets json - Android

我收到来自服务的 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"
    }
]

我用 retrofitGson 超过了 json

Problem :当 json 为空时,我该如何验证它?

[{}]

我得到 jsonGson 的值,如下所示:

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

[{}] // Wrong

纠正您来自服务器的响应。会是

[] // Perfect

你应该检查 Json Array 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();
 }

默认情况下,我认为 Gson omits null fields

因此,{}

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

其中,如果您定义了一个 new Comment(),并将其与 response.body().get(0) 进行比较,它应该是相等的。

注意:这需要您在 Comment.java

中实现 equals()hashcode() 方法