如何将JSON转换为具有特定字段的POJO。

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

How to convert json to pojo with specific fields

问题

import com.fasterxml.jackson.annotation.JsonProperty;

public class CustomPerson {

    @JsonProperty("names")
    private Names names;

    public CustomPerson() {
    }

    public String getGivenName() {
        return this.names == null ? null : this.names.getGivenName();
    }

    public String getFamilyName() {
        return this.names == null ? null : this.names.getFamilyName();
    }

    private static class Names {
        @JsonProperty("displayName")
        private String displayName;

        @JsonProperty("familyName")
        private String familyName;

        @JsonProperty("givenName")
        private String givenName;

        public String getGivenName() {
            return givenName;
        }

        public String getFamilyName() {
            return familyName;
        }
    }
}

Please make sure that you also have the necessary dependencies, such as Jackson, properly configured in your project to handle JSON serialization and deserialization.

英文:

I tried to convert this json (it's a response from google people api) to pojo. i get null instead of object with data. i think my pojo is not correct and jackson cant convert it

{
"resourceName": "people/113175645456469629209",
"etag": "%EgoBAj0DBgk+NTcuGgQBAgnfvtrUH",
"names": [
    {
        "metadata": {
            "primary": true,
            "source": {
                "type": "PROFILE",
                "id": "113175645456469629209"
            }
        },
        "displayName": "firstName lastName",
        "familyName": "lastName",
        "givenName": "firstName",
        "displayNameLastFirst": "firstName, lastName",
        "unstructuredName": "firstName, lastName"
    }
]
}

I want to get only pojo with first name and last name

import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.social.google.api.plus.Person;

public class CustomPerson {

	@JsonProperty("names")
	private CustomPerson.Names names;

	public CustomPerson() {
	}

	public String getGivenName() {
		return this.names == null ? null : this.names.givenName;
	}

	public String getFamilyName() {
		return this.names == null ? null : this.names.familyName;
	}

	private static class Names {
		@JsonProperty
		private String givenName;
		@JsonProperty
		private String familyName;

		private Names() {
		}
	}
}

any advice would help me

答案1

得分: 1

public class CustomPerson {

    @JsonProperty
    private List<CustomPerson.Names> names;

    public CustomPerson() {
    }

    private static class Names {
        @JsonProperty
        private String givenName;
        @JsonProperty
        private String familyName;

        private Names() {
        }

        public String getGivenName() {
            return this.names == null ? null : this.names.givenName;
        }

        public String getFamilyName() {
            return this.names == null ? null : this.names.familyName;
        }
    }
}
英文:
public class CustomPerson {

    @JsonProperty
    private List&lt;CustomPerson.Names&gt; names;

    public CustomPerson() {
    }

    private static class Names {
        @JsonProperty
        private String givenName;
        @JsonProperty
        private String familyName;

        private Names() {
        }

        public String getGivenName() {
            return this.names == null ? null : this.names.givenName;
        }

        public String getFamilyName() {
            return this.names == null ? null : this.names.familyName;
        }
    }
}

huangapple
  • 本文由 发表于 2020年9月18日 19:51:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63955242.html
匿名

发表评论

匿名网友

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

确定