Java - 嵌套内嵌套的Gson解析

发布于 2024-10-07 23:12:22 字数 704 浏览 2 评论 0原文

我必须与 API 进行交互,并且响应格式(根据我所读到的)似乎结构很差。我发现 Google 网上论坛回复了一个有点类似的问题 这里,但我在实现 Response 类来处理 Gson.fromJson 时遇到问题。有没有我错过的例子?

{

"response":{
    "reference": 1023, 
    "data":{
        "user":{
            "id":"210",
            "firstName":"john",
            "lastName":"smith",
            "email":"[email protected]",
            "phone":"",
            "linkedid":{
                "id":"238"
            }
        }
    }
}

}

I have to interact with an API, and the response format (from what I've read) seems to be poorly structured. I've found a google groups reply to a somewhat similiar problem here, but I'm having trouble implementing a Response class to handle the Gson.fromJson. Is there an example I'm missing that's out there?

{

"response":{
    "reference": 1023, 
    "data":{
        "user":{
            "id":"210",
            "firstName":"john",
            "lastName":"smith",
            "email":"[email protected]",
            "phone":"",
            "linkedid":{
                "id":"238"
            }
        }
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2024-10-14 23:12:22

JSON 对象 {} 可以由 Map 或 Javabean 类表示。下面是一个使用 Javabean 的示例。

public class ResponseData {
    private Response response;
    // +getter+setter

    public static class Response {
        private int reference;
        private Data data;
        // +getters+setters
    }

    public static class Data {
        private User user;
        // +getter+setter
    }

    public static class User {
        private String id;
        private String firstName; 
        private String lastName;
        private String email;
        private String phone;
        private Linkedid linkedid;
        // +getters+setters
    }

    public static class Linkedid {
        private String id;
        // +getter+setter
    }
}

使用方法如下:

ResponseData responseData = new Gson().fromJson(json, ResponseData.class);

The JSON objects {} can be represented by a Map<String, Object> or a Javabean class. Here's an example which uses a Javabean.

public class ResponseData {
    private Response response;
    // +getter+setter

    public static class Response {
        private int reference;
        private Data data;
        // +getters+setters
    }

    public static class Data {
        private User user;
        // +getter+setter
    }

    public static class User {
        private String id;
        private String firstName; 
        private String lastName;
        private String email;
        private String phone;
        private Linkedid linkedid;
        // +getters+setters
    }

    public static class Linkedid {
        private String id;
        // +getter+setter
    }
}

Use it as follows:

ResponseData responseData = new Gson().fromJson(json, ResponseData.class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文