2016-08-05 166 views
1

我想解析一个嵌套的json,其中父键包含两个不同的嵌套键对象(不是可以在列表中抽象和序列化的单个模型)。嵌套json的模型类

对于这个例子Json,什么是正确的Java模型(相应的类)?

{ 
    "name" : "Jack", 
    "surname" : "Hopper", 
    "address" : "711-2880 Nulla St. Mankato Mississippi 96522", 
    "FatherKey" : 
    { 
     "ChildKeyNo1" : 
     { 
      "defaultKey" : "2_s_g", 
      "key1" : "4_s_g", 
      "key2" : "2_s_g" 
     }, 
     "ChildKeyNo2" : 
     { 
      "defaultKey" : "4_s_g", 
      "key1" : "6_s_g", 
      "key2" : "7_s_g" 
     } 
    } 
} 

我想出:

public class MainJsonConfiguration{ 
     private String name; 
     private String surname;  
     private String address; 
     private FatherKey fatherKey; 

     public MainJsonConfiguration(String name, String surname, String address, String fatherKey){ 
      this.name = name; 
      this.surname = surname; 
      this.address = address; 
      this.fatherKey = new FatherKey(new ChildKey(), new ChildKey()); 
     } 
} 

public class FatherKey{ 

    private ChildKey childKey1; 
    private ChildKey childKey2; 

    public FatherKey(ChildKey childKey1, ChildKey childKey2){ 
     this.childKey1 = childKey1; 
     this.childKey2 = childKey2; 
    }  
} 

public class ChildKey{ 

    private String defaultKey; 
    private String key1; 
    private String key2; 

    public ChildKey(){ 
    } 
} 

,但它的气味腥....

有什么建议?

非常感谢您

+0

您可以使用FasterXML Jackson并轻松地将其解析为Map:https://github.com/FasterXML/jackson – duffymo

回答

1

您可以使用FasterXML Jackson与注释JsonProperty定义属性的名称,在构造函数注入。

这里你的类可以看起来怎么样:

public class MainJsonConfiguration{ 
    private String name; 
    private String surname; 
    private String address; 
    private FatherKey fatherKey; 

    public MainJsonConfiguration(@JsonProperty("name") String name, 
     @JsonProperty("surname") String surname, 
     @JsonProperty("address") String address, 
     @JsonProperty("FatherKey") FatherKey fatherKey){ 
     this.name = name; 
     this.surname = surname; 
     this.address = address; 
     this.fatherKey = fatherKey; 

    } 
} 

public class FatherKey{ 

    private ChildKey childKey1; 
    private ChildKey childKey2; 

    public FatherKey(@JsonProperty("ChildKeyNo1") ChildKey childKey1, 
     @JsonProperty("ChildKeyNo2") ChildKey childKey2){ 
     this.childKey1 = childKey1; 
     this.childKey2 = childKey2; 
    } 
} 

public class ChildKey{ 

    private String defaultKey; 
    private String key1; 
    private String key2; 

    public ChildKey(@JsonProperty("defaultKey") String defaultKey, 
     @JsonProperty("key1") String key1, 
     @JsonProperty("key2") String key2){ 
     this.defaultKey = defaultKey; 
     this.key1 = key1; 
     this.key2 = key2; 
    } 
} 

这里是解析您的JSON代码:

ObjectMapper mapper = new ObjectMapper(); 
MainJsonConfiguration configuration = mapper.readValue(
    jsonString, MainJsonConfiguration.class 
); 
0

GSON实施

-----------------------------------com.example.ChildKeyNo1.java----------------------------------- 

package com.example; 

import javax.annotation.Generated; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

@Generated("org.jsonschema2pojo") 
public class ChildKeyNo1 { 

@SerializedName("defaultKey") 
@Expose 
private String defaultKey; 
@SerializedName("key1") 
@Expose 
private String key1; 
@SerializedName("key2") 
@Expose 
private String key2; 

/** 
* 
* @return 
* The defaultKey 
*/ 
public String getDefaultKey() { 
return defaultKey; 
} 

/** 
* 
* @param defaultKey 
* The defaultKey 
*/ 
public void setDefaultKey(String defaultKey) { 
this.defaultKey = defaultKey; 
} 

/** 
* 
* @return 
* The key1 
*/ 
public String getKey1() { 
return key1; 
} 

/** 
* 
* @param key1 
* The key1 
*/ 
public void setKey1(String key1) { 
this.key1 = key1; 
} 

/** 
* 
* @return 
* The key2 
*/ 
public String getKey2() { 
return key2; 
} 

/** 
* 
* @param key2 
* The key2 
*/ 
public void setKey2(String key2) { 
this.key2 = key2; 
} 

} 
-----------------------------------com.example.ChildKeyNo2.java----------------------------------- 

package com.example; 

import javax.annotation.Generated; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

@Generated("org.jsonschema2pojo") 
public class ChildKeyNo2 { 

@SerializedName("defaultKey") 
@Expose 
private String defaultKey; 
@SerializedName("key1") 
@Expose 
private String key1; 
@SerializedName("key2") 
@Expose 
private String key2; 

/** 
* 
* @return 
* The defaultKey 
*/ 
public String getDefaultKey() { 
return defaultKey; 
} 

/** 
* 
* @param defaultKey 
* The defaultKey 
*/ 
public void setDefaultKey(String defaultKey) { 
this.defaultKey = defaultKey; 
} 

/** 
* 
* @return 
* The key1 
*/ 
public String getKey1() { 
return key1; 
} 

/** 
* 
* @param key1 
* The key1 
*/ 
public void setKey1(String key1) { 
this.key1 = key1; 
} 

/** 
* 
* @return 
* The key2 
*/ 
public String getKey2() { 
return key2; 
} 

/** 
* 
* @param key2 
* The key2 
*/ 
public void setKey2(String key2) { 
this.key2 = key2; 
} 

} 
-----------------------------------com.example.Example.java----------------------------------- 

package com.example; 

import javax.annotation.Generated; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

@Generated("org.jsonschema2pojo") 
public class Example { 

@SerializedName("name") 
@Expose 
private String name; 
@SerializedName("surname") 
@Expose 
private String surname; 
@SerializedName("address") 
@Expose 
private String address; 
@SerializedName("FatherKey") 
@Expose 
private FatherKey fatherKey; 

/** 
* 
* @return 
* The name 
*/ 
public String getName() { 
return name; 
} 

/** 
* 
* @param name 
* The name 
*/ 
public void setName(String name) { 
this.name = name; 
} 

/** 
* 
* @return 
* The surname 
*/ 
public String getSurname() { 
return surname; 
} 

/** 
* 
* @param surname 
* The surname 
*/ 
public void setSurname(String surname) { 
this.surname = surname; 
} 

/** 
* 
* @return 
* The address 
*/ 
public String getAddress() { 
return address; 
} 

/** 
* 
* @param address 
* The address 
*/ 
public void setAddress(String address) { 
this.address = address; 
} 

/** 
* 
* @return 
* The fatherKey 
*/ 
public FatherKey getFatherKey() { 
return fatherKey; 
} 

/** 
* 
* @param fatherKey 
* The FatherKey 
*/ 
public void setFatherKey(FatherKey fatherKey) { 
this.fatherKey = fatherKey; 
} 

} 
-----------------------------------com.example.FatherKey.java----------------------------------- 

package com.example; 

import javax.annotation.Generated; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

@Generated("org.jsonschema2pojo") 
public class FatherKey { 

@SerializedName("ChildKeyNo1") 
@Expose 
private ChildKeyNo1 childKeyNo1; 
@SerializedName("ChildKeyNo2") 
@Expose 
private ChildKeyNo2 childKeyNo2; 

/** 
* 
* @return 
* The childKeyNo1 
*/ 
public ChildKeyNo1 getChildKeyNo1() { 
return childKeyNo1; 
} 

/** 
* 
* @param childKeyNo1 
* The ChildKeyNo1 
*/ 
public void setChildKeyNo1(ChildKeyNo1 childKeyNo1) { 
this.childKeyNo1 = childKeyNo1; 
} 

/** 
* 
* @return 
* The childKeyNo2 
*/ 
public ChildKeyNo2 getChildKeyNo2() { 
return childKeyNo2; 
} 

/** 
* 
* @param childKeyNo2 
* The ChildKeyNo2 
*/ 
public void setChildKeyNo2(ChildKeyNo2 childKeyNo2) { 
this.childKeyNo2 = childKeyNo2; 
} 

} 

然后做这样的事情

Gson gson = new Gson(); 
Example ex = gson.fromJson(jsonString,Example.class);