猿问

如何从gson获取嵌套数据?

我向外部 RESTful api 发出了一个 get 请求,并收到一个具有此结构的 json 对象作为响应:


{

    "data": {

       "id": 1,

       "name": "John Doe",

       "email": "doe@john.com",

       "urlPicture": "urlPicture.com/82819",

       "address": {

          "street": "My street",

          "number": "29",

          "city": "Nurnberg",

          "country": "Germany"

       }

    }

}

我不需要此响应的所有内容,我只想将一些字段保存在数据库中。


我的 POJO 类类似于此伪代码:



    public class Data{

        private User user;


        // getters and setters

    }


    public class User{

        private int id;

        private String name;

        private String urlPicture;

        private String country;


        // getters and setters

    }


但是,当我尝试提取我想要的字段时,我在这个字段中收到 null



    public void testResponse(){

        RestTemplate restTemplate = new RestTemplate();

        Data data = new Data();

        User user = new User();

        Gson gson = new Gson();


        String response = restTemplate.getForObject(

                    "https://apifrommyrequest.com/user/{id}",

                    String.class,

                    73442);

        user = gson.fromJson(response, User.class);

        System.out.println(user);

    }

    ```


My output:

22:20:57.641 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://apifrommyrequest.com/user/73442

22:20:57.672 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]

22:20:58.243 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK

22:20:58.247 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [java.lang.String] as "application/json;charset=UTF-8"

Data(id=0, name=null, urlPicture=null, country=null)


I really don't know how to do anymore.


慕哥9229398
浏览 111回答 3
3回答

蛊毒传说

试试下面的代码。您不能直接访问国家/地区,它是放置在嵌套对象中的值。而且由于响应主体作为数据返回,因此您无法将其转换为用户对象。首先,您需要将其转换为数据对象。public class User {    private int id;    private String name;    private String urlPicture;    private Address address;    // getters and setters}public class Address {    private String country;    // getters and setters}public void testResponse(){    RestTemplate restTemplate = new RestTemplate();    Data data = new Data();    User user = new User();    Gson gson = new Gson();    String response = restTemplate.getForObject(                "https://apifrommyrequest.com/user/{id}",                String.class,                73442);    data = gson.fromJson(response, Data.class);    System.out.println(data);}

明月笑刀无情

如果你愿意,你也可以像这样设计你的 POJO(以减少嵌套的 POJO):public class Data {&nbsp; &nbsp; private User data;&nbsp; &nbsp;//getters and setters}public class User {&nbsp; &nbsp; private int id;&nbsp; &nbsp; private String name;&nbsp; &nbsp; private String urlPicture;&nbsp; &nbsp; private String country;&nbsp; &nbsp; @JsonProperty("address")&nbsp; &nbsp; private void unpackNested(Map<String,String> elements)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.country = elements.get("country");&nbsp; &nbsp; }&nbsp; &nbsp;//getters and setters}然后最后在 Data 类上反序列化

慕慕森

你真的不需要做从字符串到对象的转换,resttemplate 已经使用 jackson 为你做了,只需要YourObject response = restTemplate.getForObject(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://apifrommyrequest.com/user/{id}",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; YourObject.class,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 73442);然后它将映射到你的 pojo 对象,在这种情况下你真的不需要 gson。
随时随地看视频慕课网APP

相关分类

Java
我要回答