我想解析一个嵌套的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 :(得分: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
);
答案 1 :(得分: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);