使用GSON和Java正确解析JSON。

huangapple go评论103阅读模式
英文:

Parsing json correctly using GSON and Java

问题

以下是您要翻译的内容:

  1. public class Result {
  2. String id, user_id, title, first_name, last_name, gender, dob, email, phone,
  3. website, address, status;
  4. Links _links;
  5. }
  6. public class Links {
  7. Link self, edit, avatar;
  8. }
  9. public class Link {
  10. String href;
  11. }
英文:

This is my json string as I want to convert it to Java Class:

  1. "result": {
  2. "id": "39559",
  3. "first_name": "John",
  4. "last_name": "Who",
  5. "gender": "male",
  6. "dob": null,
  7. "email": "john.who@roberts.com",
  8. "phone": null,
  9. "website": null,
  10. "address": null,
  11. "status": "active",
  12. "_links": {
  13. "self": {
  14. "href": "https://gorest.co.in/public-api/users/39559"
  15. },
  16. "edit": {
  17. "href": "https://gorest.co.in/public-api/users/39559"
  18. },
  19. "avatar": {
  20. "href": null
  21. }
  22. }
  23. }

And this is my class (I removed the getters and toString):

  1. public class Result {
  2. String id, user_id, title, first_name, last_name, gender, dob, email, phone,
  3. website, address, status, _links, album_id, url, thumbnail, post_id, name, body;
  4. }

I wonder how can I represent

  1. "_links": {
  2. "self": {
  3. "href": "https://gorest.co.in/public-api/users/39559"
  4. },
  5. "edit": {
  6. "href": "https://gorest.co.in/public-api/users/39559"
  7. },
  8. "avatar": {
  9. "href": null
  10. }
  11. }

How can I represent the missing part in my Result class?

答案1

得分: 0

_links似乎是一个具有selfeditavatar属性的JSON对象。这些属性中的每一个也都是JSON对象,都具有href属性。→ 您需要这三个类来表示这些对象:

  1. public class Result {
  2. String id, user_id, title, first_name, last_name, gender;
  3. String dob, email, phone, website, address, status;
  4. String album_id, url, thumbnail, post_id, name, body;
  5. Links _links;
  6. }
  1. public class Links {
  2. Link self, edit, avatar;
  3. }
  1. public class Link {
  2. String href;
  3. }
英文:

_links seems to be a JSON Object with attributes self, edit and avatar. Each of these attributes are JSON Objects too, all having attribute href. → You need these three classes for representing those objects:

  1. public class Result {
  2. String id, user_id, title, first_name, last_name, gender;
  3. String dob, email, phone, website, address, status;
  4. String album_id, url, thumbnail, post_id, name, body;
  5. Links _links;
  6. }
  1. public class Links {
  2. Link self, edit, avatar;
  3. }
  1. public class Link {
  2. String href;
  3. }

答案2

得分: 0

你需要创建一个具有字段的链接对象。

英文:

you need to create Links as object with fields

huangapple
  • 本文由 发表于 2020年6月29日 03:25:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/62627280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定