如何使用泛型类型从休息调用解析json列表?

时间:2016-10-14 21:36:41

标签: java json rest

我正在尝试解析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"
      }
    ]
  }
}

我正在使用下面的代码集来接收来自Rest的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和版本,但我在null中收到port_desc。有人可以解释我做错了吗?

1 个答案:

答案 0 :(得分:1)

将属性的名称重命名为Json字段名称。例如portDesc - &gt; port_desc