我需要将以下JSON
格式读入我的模型类中,我能够做到但我想删除知道不同分支位置的依赖关系,例如nyc
,{{1 }}
我很困惑如何修改此实现以读取JSONObject
我想将其设为boston
,以便它可以在不更改代码的情况下读取新的分支信息。示例:Generic
。其他JSON结构仍然是我的标准。我在charlotte
课程中使用Builder
模式。
注意:如果有任何建议要修改Model
结构以容纳新位置,那么实施变为JSON
我愿意讨论。
这是我正在研究的JSON:
Generic
以下是我的模型类:
{
"company": "abc inc",
"logoUrl": "someUrl",
"phone": "1234567890",
"branch": {
"nyc": {
"products": {
"asian": {
"somekey1": "someValue1",
"somekeyN": "somevalueN"
},
"american": {
"somekey1": "someValue1",
"somekeyN": "somevalueN"
}
}
},
"boston": {
"products": {
"asian": {
"somekey1": "somevalue1",
"somekeyN": "somevalueN"
},
"american": {
"somekey1": "somevalue1",
"somekeyN": "somevalueN"
}
}
},
"charlotte": {
"products": {
"asian": {
"somekey1": "somevalue1",
"somekeyN": "somevalueN"
},
"american": {
"somekey1": "somevalue1",
"somekeyN": "somevalueN"
}
}
}
}
}
在@JsonDeserialize( builder = Content.Builder.class)
public class Content {
private final String company;
private final String logoUrl;
private final String phone;
private final Branch branch;
private Content(Builder builder){
this.company = builder.company;
this.logoUrl = builder.logoUrl;
this.phone = builder.phone;
this.branch = builder.branch;
}
public String getCompany() {
return company;
}
public String getLogoUrl() {
return logoUrl;
}
public String getPhone() {
return phone;
}
public Branch getBranch() {
return branch;
}
@JsonIgnoreProperties( ignoreUnknown = true)
public static class Builder {
private String company;
private String logoUrl;
private String phone;
private Branch branch;
@JsonProperty("company")
public Builder withCompany(String company){
this.company = company;
return this;
}
@JsonProperty("logoUrl")
public Builder withLogoUrl(String logoUrl){
this.logoUrl = logoUrl;
return this;
}
@JsonProperty("phone")
public Builder withPhone(String phone){
this.phone = phone;
return this;
}
@JsonProperty("branch")
public Builder withBranch(Branch branch) {
this.branch = branch;
return this;
}
public Content build(){
return new Content(this);
}
}
}
模型类中,我想删除添加新位置示例Branch
的依赖关系并制作此Generic。如何才能完成这项工作,以便我不需要使用charlotte
,并且会读取和映射任何新位置。
@JsonProperty("nyc")
其他模特课程:
@JsonDeserialize( builder = Branch.Builder.class)
public class Branch {
private final Nyc nyc;
private final Boston boston;
private Branch(Builder builder){
this.boston = builder.boston;
this.nyc = builder.nyc;
}
public Nyc getNyc() {
return nyc;
}
public Boston getBoston() {
return boston;
}
@JsonIgnoreProperties( ignoreUnknown = true)
public static class Builder{
private Nyc nyc;
private Boston boston;
@JsonProperty("nyc")
public Builder withNyc(Nyc nyc){
this.nyc = nyc;
return this;
}
@JsonProperty("boston")
public Builder withBoston(Boston boston){
this.boston = boston;
return this;
}
public Branch build(){
return new Branch(this);
}
}
}
@JsonDeserialize(builder = Products.Builder.class)
public class Products {
private final Map<String,String> asian;
private final Map<String,String> american;
public Products(Builder builder){
this.asian = builder.asian;
this.american = builder.american;
}
public Map<String, String> getAsian() {
return asian;
}
public Map<String, String> getAmerican() {
return american;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Builder {
private Map<String,String> asian;
private Map<String,String> american;
@JsonProperty("asian")
public Builder withAsian(Map<String,String> asian){
this.asian = asian;
return this;
}
@JsonProperty("american")
public Builder withAmerican(Map<String,String> american){
this.american = american;
return this;
}
public Products build(){
return new Products(this);
}
}
}
主:
@JsonDeserialize(builder = Boston.Builder.class)
public class Boston {
private final Products products;
private Boston(Builder builder){
this.products = builder.products;
}
public Products getProducts() {
return products;
}
@JsonIgnoreProperties( ignoreUnknown = true)
public static class Builder {
private Products products;
@JsonProperty("products")
public Builder withProducts(Products products){
this.products = products;
return this;
}
public Boston build(){
return new Boston(this);
}
}
}
@JsonDeserialize(builder = Nyc.Builder.class)
public class Nyc {
private final Products products;
private Nyc(Builder builder){
this.products = builder.products;
}
public Products getProducts() {
return products;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Builder{
private Products products;
@JsonProperty("products")
public Builder withProducts(Products products){
this.products = products;
return this;
}
public Nyc build(){
return new Nyc(this);
}
}
}