Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 嵌套json的模型类_Java_Json - Fatal编程技术网

Java 嵌套json的模型类

Java 嵌套json的模型类,java,json,Java,Json,我想解析一个嵌套的json,其中父键包含两个不同的嵌套键对象(不是一个可以在列表中抽象和序列化的模型) 对于这个示例Json,正确的Java模型(对应的类)是什么 { "name" : "Jack", "surname" : "Hopper", "address" : "711-2880 Nulla St. Mankato Mississippi 96522", "FatherKey" : { "ChildKeyNo1" :

我想解析一个嵌套的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(){
    }
}
但它闻起来有鱼腥味

有什么建议吗

非常感谢

您可以使用注释
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
);
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);

您可以使用FasterXML Jackson并轻松地将其解析为地图: