2016-10-14 15 views
1

私はJSONの下に解析しようとしています:私は残りの部分からJSONを受け取るためのコードのセットの下に使用していますジェネリック型を使ってjson listをrest callから解析する方法は?

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

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


} 

:以下

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

は私のJavaクラスです00:00:08:00:27:5d:39:55とバージョンを解析できますが、nullport_descにあります。誰かが私が間違っていることを説明することはできますか?

+1

トピック以外のヒント:適切な回答を得る機会を増やしたい場合は、例を最小限に抑えてみてください。多くの人々が何百ものコード行をデバッグしようとしていません。詳細については、[SSCCE](http://sscce.org)/ [MCVE]を参照してください。また、このような最小限の例を作成すると、問題の原因を見つけることさえ可能性が高くなります。 – Pshemo

答えて

1

プロパティの名前をJsonのフィールド名に変更します。例えばportDesc - > port_desc

+0

ありがとうございます@ Jianping!その理由を教えてもらえますか... –

+1

jsonのテキストとJavaクラスで同じ名前を持たなければならないからです。さもなければデシリアライザはそれらを正しくマップすることができません。 –

+0

http://pojo.sodhanalibrary.com/を使用してjsonをJavaクラスに変換できます –

関連する問題