2016-10-14 37 views
1

我試圖解析以下JSON:如何使用泛型類型從休息調用解析json列表?

{ 
    "00:00:08:00:27:5d:39:55": { 
    "version": "OF_13", 
    "port_desc": [ 
     { 
     "port_number": "local", 
     "hardware_address": "08:00:27:5d:39:55", 
     "name": "flips", 
     "config": [ 
      "PORT_DOWN" 
     ], 
     "state": [ 
      "LINK_DOWN" 
     ], 
     "current_features": [], 
     "advertised_features": [], 
     "supported_features": [], 
     "peer_features": [], 
     "curr_speed": "0", 
     "max_speed": "0" 
     }, 
     { 
     "port_number": "1", 
     "hardware_address": "08:00:27:5d:39:55", 
     "name": "eth1", 
     "config": [], 
     "state": [], 
     "current_features": [ 
      "PF_1GB_FD", 
      "PF_COPPER", 
      "PF_AUTONEG" 
     ], 
     "advertised_features": [ 
      "PF_10MB_HD", 
      "PF_10MB_FD", 
      "PF_100MB_HD", 
      "PF_100MB_FD", 
      "PF_1GB_FD", 
      "PF_COPPER", 
      "PF_AUTONEG" 
     ], 
     "supported_features": [ 
      "PF_10MB_HD", 
      "PF_10MB_FD", 
      "PF_100MB_HD", 
      "PF_100MB_FD", 
      "PF_1GB_FD", 
      "PF_COPPER", 
      "PF_AUTONEG" 
     ], 
     "peer_features": [], 
     "curr_speed": "1000000", 
     "max_speed": "1000000" 
     }, 
     { 
     "port_number": "2", 
     "hardware_address": "08:00:27:86:14:71", 
     "name": "eth2", 
     "config": [], 
     "state": [], 
     "current_features": [ 
      "PF_1GB_FD", 
      "PF_COPPER", 
      "PF_AUTONEG" 
     ], 
     "advertised_features": [ 
      "PF_10MB_HD", 
      "PF_10MB_FD", 
      "PF_100MB_HD", 
      "PF_100MB_FD", 
      "PF_1GB_FD", 
      "PF_COPPER", 
      "PF_AUTONEG" 
     ], 
     "supported_features": [ 
      "PF_10MB_HD", 
      "PF_10MB_FD", 
      "PF_100MB_HD", 
      "PF_100MB_FD", 
      "PF_1GB_FD", 
      "PF_COPPER", 
      "PF_AUTONEG" 
     ], 
     "peer_features": [], 
     "curr_speed": "1000000", 
     "max_speed": "1000000" 
     }, 
     { 
     "port_number": "3", 
     "hardware_address": "08:00:27:aa:7d:02", 
     "name": "eth3", 
     "config": [], 
     "state": [], 
     "current_features": [ 
      "PF_1GB_FD", 
      "PF_COPPER", 
      "PF_AUTONEG" 
     ], 
     "advertised_features": [ 
      "PF_10MB_HD", 
      "PF_10MB_FD", 
      "PF_100MB_HD", 
      "PF_100MB_FD", 
      "PF_1GB_FD", 
      "PF_COPPER", 
      "PF_AUTONEG" 
     ], 
     "supported_features": [ 
      "PF_10MB_HD", 
      "PF_10MB_FD", 
      "PF_100MB_HD", 
      "PF_100MB_FD", 
      "PF_1GB_FD", 
      "PF_COPPER", 
      "PF_AUTONEG" 
     ], 
     "peer_features": [], 
     "curr_speed": "1000000", 
     "max_speed": "1000000" 
     } 
    ] 
    } 
} 

我使用下面的代碼集從靜止收到JSON:

Map<String, SwitchAllJson> switchMap = response.getEntity(new GenericType<Map<String, SwitchAllJson>>(){}); 

下面是我的Java類:

public class SwitchAllJson { 
    private String version; 
    private List<PortDescriptionJson> portDesc =new ArrayList<PortDescriptionJson>(); 

    public String getVersion() { 
     return version; 
    } 
    public void setVersion(String version) { 
     this.version = version; 
    } 
    public List<PortDescriptionJson> getPortDesc() { 
     return portDesc; 
    } 
    public void setPortDesc(List<PortDescriptionJson> portDesc) { 
     this.portDesc = portDesc; 
    } 

} 


public class PortDescriptionJson { 

     private String portNumber; 
     private String hardwareAddress; 
     private String name; 
     private List<String> config; 
     private List<String> state; 
     private List<String> currentFeatures; 
     private List<String> advertisedFeatures; 
     private List<String> supportedFeatures; 
     private List<String> peerFeatures; 
     private String currSpeed; 
     private String maxSpeed; 

    public String getHardwareAddress(){ 
     return this.hardwareAddress; 
    } 

    public String getPortNumber(){ 
     return this.portNumber; 
    } 

    public void setHardwareAddress(String hardwareAddress){ 
     this.hardwareAddress=hardwareAddress; 
    } 

    public void setPortNumber(String portNumber){ 
     this.portNumber=portNumber; 
    } 


    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public List<String> getConfig() { 
     return config; 
    } 

    public void setConfig(List<String> config) { 
     this.config = config; 
    } 

    public List<String> getState() { 
     return state; 
    } 

    public void setState(List<String> state) { 
     this.state = state; 
    } 

    public List<String> getCurrentFeatures() { 
     return currentFeatures; 
    } 

    public void setCurrentFeatures(List<String> currentFeatures) { 
     this.currentFeatures = currentFeatures; 
    } 

    public List<String> getAdvertisedFeatures() { 
     return advertisedFeatures; 
    } 

    public void setAdvertisedFeatures(List<String> advertisedFeatures) { 
     this.advertisedFeatures = advertisedFeatures; 
    } 

    public List<String> getSupportedFeatures() { 
     return supportedFeatures; 
    } 

    public void setSupportedFeatures(List<String> supportedFeatures) { 
     this.supportedFeatures = supportedFeatures; 
    } 

    public List<String> getPeerFeatures() { 
     return peerFeatures; 
    } 

    public void setPeer_features(List<String> peerFeatures) { 
     this.peerFeatures = peerFeatures; 
    } 

    public String getCurrSpeed() { 
     return currSpeed; 
    } 

    public void setCurrSpeed(String currSpeed) { 
     this.currSpeed = currSpeed; 
    } 

    public String getMaxSpeed() { 
     return maxSpeed; 
    } 

    public void setMaxSpeed(String maxSpeed) { 
     this.maxSpeed = maxSpeed; 
    } 


} 

我能夠解析00:00:08:00:27:5d:39:55和版本,但我得到nullport_desc。有人能解釋我做錯了什麼嗎?

+1

離題提示:如果您想增加獲得正確答案的機會,請嘗試將您的示例限制爲最小。沒有多少人願意調試數百個代碼行。詳細瞭解:[SSCCE](http://sscce.org)/ [MCVE]。當創建這樣一個最小的例子時,你甚至很有可能找到你的問題的原因。 – Pshemo

回答

1

將屬性名稱重命名爲Json字段名稱。例如portDesc - > port_desc

+0

謝謝@建平!你能告訴我爲什麼那麼工作...... –

+1

這是因爲你必須在json文本和java類中有相同的名稱,否則,除鏽劑將無法正確映射它們。 –

+0

您可以使用http://pojo.sodhanalibrary.com/將json轉換爲java類 –