2016-08-05 4 views
1

Ich möchte einen verschachtelten JSON analysieren, wobei der Vaterschlüssel zwei verschiedene verschachtelte Schlüsselobjekte enthält (kein einzelnes Modell, das in einer Liste abstrahiert und serialisiert werden könnte).Modellklasse für verschachtelte JSON

Was wäre ein korrektes Java-Modell (entsprechende Klasse) für dieses Beispiel Json?

{ 
    "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" 
     } 
    } 
} 

Ich habe kommen mit:

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(){ 
    } 
} 

aber es riecht fischig ....

irgendwelche Vorschläge?

Vielen Dank

+0

Sie können FasterXML Jackson verwenden und es leicht zu einer Karte analysieren: https://github.com/FasterXML/jackson – duffymo

Antwort

1

Sie FasterXML Jackson mit dem Vermerk JsonProperty die Namen der Eigenschaften definieren können in der Konstrukteurs zu injizieren.

Hier, wie Sie Ihre Klasse aussehen könnte:

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; 
    } 
} 

Hier ist der Code Ihre JSON zu analysieren:

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

Gson Implementierung

-----------------------------------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; 
} 

} 

dann wie etwas tun dies

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