使用GSON和Java正确解析JSON。

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

Parsing json correctly using GSON and Java

问题

以下是您要翻译的内容:

public class Result {
    String id, user_id, title, first_name, last_name, gender, dob, email, phone,
            website, address, status;
    Links _links;
}

public class Links {
    Link self, edit, avatar;
}

public class Link {
    String href;
}
英文:

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

"result": {
    "id": "39559",
    "first_name": "John",
    "last_name": "Who",
    "gender": "male",
    "dob": null,
    "email": "john.who@roberts.com",
    "phone": null,
    "website": null,
    "address": null,
    "status": "active",
    "_links": {
        "self": {
            "href": "https://gorest.co.in/public-api/users/39559"
        },
        "edit": {
            "href": "https://gorest.co.in/public-api/users/39559"
        },
        "avatar": {
            "href": null
        }
    }
}

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

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

I wonder how can I represent

"_links": {
    "self": {
        "href": "https://gorest.co.in/public-api/users/39559"
    },
    "edit": {
        "href": "https://gorest.co.in/public-api/users/39559"
    },
    "avatar": {
        "href": null
    }
}

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

答案1

得分: 0

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

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

_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:

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

答案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:

确定