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

如何处理Registfit2.0响应中的JSON空对象?

闻人思聪
2023-03-14

下面是一个json响应的示例。正如您所看到的,像:news、number_likes、number_comment、number_post、user和owner这样的字段总是存在。但是像:place、place_mark、comment、album、photo这样的字段并不总是存在。这取决于新闻的类型。

{
"result": true,
"news_list": [
{
"news": {
"id": "67",
"text": "",
"text_post": "",
"date_create": "1460321713"
},
"number_likes": "0",
"number_comment": "0",
"number_post": "0",
"user": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/08382.jpeg",
"last_activity": "1460035798"
},
"owner": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/08382.jpeg",
"last_activity": "1460035798"
},
"place": {
"id": "1",
"name": "test",
"avatar": ""
},
"place_mark": {
"id": "1",
"date_create": "1460321699"
}
},
{
"news": {
"id": "66",
"text": "",
"text_post": "",
"date_create": "1460321623"
},
"number_likes": "0",
"number_comment": "0",
"number_post": "0",
"user": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/08382.jpeg",
"last_activity": "1460035798"
},
"owner": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/08082.jpeg",
"last_activity": "1460035798"
},
"place": {
"id": "1",
"name": "test",
"avatar": ""
},
"comment": {
"id": "1",
"parent_id": "",
"text": "qwer",
"rating": "0",
"date_create": "1460321472"
},
"photos": []
},
{
"news": {
"id": "65",
"text": "new news",
"text_post": "",
"date_create": "1460320767"
},
"number_likes": "0",
"number_comment": "0",
"number_post": "0",
"user": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/08082.jpeg",
"last_activity": "1460035798"
},
"owner": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/0882.jpeg",
"last_activity": "1460035798"
},
"photos": [
{
"photo": {
"id": "84",
"name": "",
"filename": "2d20f883cb023a1ff14f32d0ee79a02c.jpeg",
"mime_type": "image/jpeg",
"height": "540",
"width": "442",
"link": "http://base_url/userPhotos/41/album/original/29a02c.jpeg"
}
},
{
"photo": {
"id": "85",
"name": "",
"filename": "2d20f883cb023a1ff14f32d0ee79a02c.jpeg",
"mime_type": "image/jpeg",
"height": "540",
"width": "442",
"link": "http://base_url/userPhotos/41/album/original/2d20f2c.jpeg"
}
}
]
},
{
"news": {
"id": "64",
"text": "",
"text_post": "",
"date_create": "1460320759"
},
"number_likes": "0",
"number_comment": "0",
"number_post": "0",
"user": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/0809fb.jpeg",
"last_activity": "1460035798"
},
"owner": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/0882.jpeg",
"last_activity": "1460035798"
},
"album": {
"id": "34",
"name": "asdfgh",
"description": "",
"position": ""
},
"photo": ""
}
]
}
public class GetNewsResponse {

private String result;

private List<NewsList> news_list;

private Errors errors;

public String getResult() {
    return result;
}

public List<NewsList> getNews_list() {
    return news_list;
}

public Errors getErrors() {
    return errors;
}


public class NewsList {

    private String number_likes;

    private String number_comment;

    private String number_post;

    private News news;

    private User user;

    private Owner owner;

    private Place place;

    private PlaceMark place_mark;

    private Comment comment;

    private Album album;

    private List<Photo2> photos;

    public String getNumber_likes() {
        return number_likes;
    }

    public String getNumber_comment() {
        return number_comment;
    }

    public String getNumber_post() {
        return number_post;
    }

    public News getNews() {
        return news;
    }

    public User getUser() {
        return user;
    }

    public Owner getOwner() {
        return owner;
    }

    public Place getPlace() {
        return place;
    }

    public PlaceMark getPlaceMark() {
        return place_mark;
    }

    public Comment getComment() {
        return comment;
    }

    public Album getAlbum() {
        return album;
    }

    public List<Photo2> getPhotos() {
            return photos;
    }
}


public class News {

    private String id;

    private String text;

    private String text_post;

    private String date_create;

    public String getId() {
        return id;
    }

    public String getText() {
        return text;
    }

    public String getDate_create() {
        return date_create;
    }

    public String getText_post() {
        return text_post;
    }
}

public class User {

    private String id;

    private String name;

    private String last_name;

    private String avatar;

    private String last_activity;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getLast_name() {
        return last_name;
    }

    public String getAvatar() {
        return avatar;
    }

    public String getLast_activity() {
        return last_activity;
    }

}

public class Owner {

    private String id;

    private String name;

    private String last_name;

    private String avatar;

    private String last_activity;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getLast_name() {
        return last_name;
    }

    public String getAvatar() {
        return avatar;
    }

    public String getLast_activity() {
        return last_activity;
    }
}

public class Place {

    private String id;

    private String name;

    private String avatar;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getAvatar() {
        return avatar;
    }
}

public class PlaceMark {

    private String id;

    private String date_create;

    public String getId() {
        return id;
    }

    public String getDate_create() {
        return date_create;
    }
}

public class Comment {

    private String id;

    private String parent_id;

    private String text;

    private String rating;

    private String date_create;

    public String getId() {
        return id;
    }

    public String getParent_id() {
        return parent_id;
    }

    public String getRating() {
        return rating;
    }

    public String getText() {
        return text;
    }

    public String getDate_create() {
        return date_create;
    }
}

public class Album {

    private String id;

    private String name;

    private String description;

    private String position;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public String getPosition() {
        return position;
    }

}

public class Photo2 {

    private Object photo;

    public Object getPhoto() {
        return photo;
    }
}

public class Photo {

    private String id;

    private String name;

    private String filename;

    private String mime_type;

    private String height;

    private String width;

    private String link;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getFilename() {
        return filename;
    }

    public String getMime_type() {
        return mime_type;
    }

    public String getHeight() {
        return height;
    }

    public String getWidth() {
        return width;
    }

    public String getLink() {
        return link;
    }
}

public class Errors {

    private String text;

    public String getText() {
        return text;
    }

}
}
public void getNewsMethod() {
sm.getNews(user_id, limit, offset, photo_size, avatar_size, all, new Callback(){

        @Override
        public void onResponse(Call<GetNewsResponse> call, Response<GetNewsResponse> response) {
            List<GetNewsResponse.NewsList> list = response.body().getNews_list();
            instantiateAdapter(list);
        }

        @Override
        public void onFailure(Call<GetNewsResponse> call, Throwable t) {
            Toast.makeText(MyNewsActivity.this, "get news failure", Toast.LENGTH_SHORT).show();
        }
    });
}
private static Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());

请帮帮我!为了正确的json反序列化,我需要做什么?也许有人有经验,在编写自定义转炉工厂或可以给我任何建议,根据我的问题?谢了。

共有1个答案

郎言
2023-03-14

对您的json响应进行一次掠夺。上一个NewsList对象有问题。

{
"news": {
"id": "64",
"text": "",
"text_post": "",
"date_create": "1460320759"
},
"number_likes": "0",
"number_comment": "0",
"number_post": "0",
"user": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/0809fb.jpeg",
"last_activity": "1460035798"
},
"owner": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/0882.jpeg",
"last_activity": "1460035798"
},
"album": {
"id": "34",
"name": "asdfgh",
"description": "",
"position": ""
},
"photo": "" // <- the problem
}

你应该收到一个名单o照片,根据您的规格。但你收到了一张带绳子的标签照片。请检查您的网络服务。

 类似资料:
  • 问题内容: 我有一个页面作为HtmlUnit中的UnexpectedPage返回,响应为JSON。我可以使用HTMLUnit进行解析还是需要一个额外的库? 问题答案: HtmlUnit不支持它。它最多可以执行JS函数。您需要事先检查返回的响应是否匹配,然后使用合适的工具进行解析。Google Gson 在此方面很有用。 如果JSON结构是事先已知的,则您甚至可以使用Gson将其转换为完全有价值的J

  • 我的授权服务在成功时返回http 204,在失败时返回http 401,但没有响应库。我无法使用RestTemplate客户端使用此功能。尝试序列化响应失败。来自Jackson的错误建议我在序列化程序中打开FAIL_on_EMPTY_bean,但是如何在restTemplate中设置它呢 使用RESTAPI的客户端 其余服务 例外 尝试的修复 下一个错误

  • 我面临一个问题,有时Json响应返回一个对象数组,有时是对象本身,我们如何在响应类中动态处理。在当前的eg:结果有时会得到一个对象数组 有时对象本身 例如: 我们如何处理这件事? 请协助... 有没有人能给点建议用不同的罐子来实现这个?

  • 问题内容: 我在弄清楚如何从jQuery $ .post()请求中正确读取JSON响应时遇到了麻烦。 在下面的jQuery代码中,我根据对应的“ color_entry_id”(用作键)从DOM中的元素填充字符串的关联数组: 然后,我发出POST请求,发送“ image_links”数组: 另外,如上所示,我尝试遍历响应数组并输出要成为字符串的每个项目,但是我只得到“ [object Object

  • 这是我在改装2.0中获得回应的逻辑 现在他们对API进行了更改,数据如下: 如何处理ONResponse中的对象并解析数组中的钱包信息?

  • 问题内容: 我的网络服务返回一个JSON对象,如下所示 即,当我将此地址放入chrome浏览器中时,我可以到达上面。 我正在尝试在浏览器中阅读此内容,以便可以创建一个下拉选项…但是从以下代码开始我一无所获: 我总是在警报框中。我用fiddler2再次检查了Web服务请求/响应是否正常,我什至可以拦截Web服务和浏览器之间的json对象。 我也试过 我又得到了空。 我已经看过Ajax要求的json响