Gson:POJO没有从JSON生成

时间:2014-07-28 09:42:40

标签: java json pojo

这是我的架构类Server.java - >

@Generated("org.jsonschema2pojo")
public class Server {

private String status;
private String updated;
private String hostId;
private String oSEXTSRVATTRHost;
private Networks addresses;
private List<Link> links = new ArrayList<Link>();
private Object keyName;
private Image image;
private Object oSEXTSTSTaskState;
private String oSEXTSTSVmState;
private String oSEXTSRVATTRInstanceName;
private String oSSRVUSGLaunchedAt;
private String oSEXTSRVATTRHypervisorHostname;
private Flavor flavor;
private String id;
private List<SecurityGroup> securityGroups = new ArrayList<SecurityGroup>();
private Object oSSRVUSGTerminatedAt;
private String oSEXTAZAvailabilityZone;
private String userId;
private String name;
private String created;
private String tenantId;
private String oSDCFDiskConfig;
private List<Object> osExtendedVolumesVolumesAttached = new ArrayList<Object>();
private String accessIPv4;
private String accessIPv6;
private Integer progress;
private Integer oSEXTSTSPowerState;
private String configDrive;
private Metadata metadata;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public String getStatus() {
return status;
}
. ..
 More getters and setters here.
. ..
}

这是我正在尝试生成POJO的JSON字符串: -

{
"server": {
    "status": "ACTIVE",
    "updated": "2014-07-18T09:55:14Z",
    "hostId": "872e9b93ccd50b93612d49f4c371f49fe6365f02bcb51994868eb88f",
    "OS-EXT-SRV-ATTR:host": "controller",
    "addresses": {
        "officenet": [
            {
                "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:25:0d:c8",
                "version": 4,
                "addr": "10.43.1.183",
                "OS-EXT-IPS:type": "fixed"
            }
        ]
    },
    "links": [
        {
            "href": "http://10.43.1.197:8774/v2/1333f00f05564a02b3c4f608b9622fc9/servers/4e357d2e-b3b1-4160-95b9-fc9102a375d9",
            "rel": "self"
        },
        {
            "href": "http://10.43.1.197:8774/1333f00f05564a02b3c4f608b9622fc9/servers/4e357d2e-b3b1-4160-95b9-fc9102a375d9",
            "rel": "bookmark"
        }
    ],
    "key_name": null,
    "image": {
        "id": "b724ae53-9113-4e33-987a-f58fd9a4ec81",
        "links": [
            {
                "href": "http://10.43.1.197:8774/1333f00f05564a02b3c4f608b9622fc9/images/b724ae53-9113-4e33-987a-f58fd9a4ec81",
                "rel": "bookmark"
            }
        ]
    },
    "OS-EXT-STS:task_state": null,
    "OS-EXT-STS:vm_state": "active",
    "OS-EXT-SRV-ATTR:instance_name": "instance-00000017",
    "OS-SRV-USG:launched_at": "2014-07-11T11:43:41.000000",
    "OS-EXT-SRV-ATTR:hypervisor_hostname": "controller",
    "flavor": {
        "id": "2",
        "links": [
            {
                "href": "http://10.43.1.197:8774/1333f00f05564a02b3c4f608b9622fc9/flavors/2",
                "rel": "bookmark"
            }
        ]
    },
    "id": "4e357d2e-b3b1-4160-95b9-fc9102a375d9",
    "security_groups": [
        {
            "name": "default"
        }
    ],
    "OS-SRV-USG:terminated_at": null,
    "OS-EXT-AZ:availability_zone": "nova",
    "user_id": "a859477b42794e8fa6dbf339361bb268",
    "name": "Akshay-Debian-2",
    "created": "2014-07-11T11:43:35Z",
    "tenant_id": "1333f00f05564a02b3c4f608b9622fc9",
    "OS-DCF:diskConfig": "MANUAL",
    "os-extended-volumes:volumes_attached": [],
    "accessIPv4": "",
    "accessIPv6": "",
    "progress": 0,
    "OS-EXT-STS:power_state": 1,
    "config_drive": "",
    "metadata": {}
}

}

这是我的主要代码,它使用Gson从JSON字符串生成POJO: -

public class ServerDataMapper {


    public static void main(String Args[]) {

        String myJSONString = "{\n    \"server\": {\n        \"status\": \"ACTIVE\",\n        \"updated\": \"2014-07-18T09:55:14Z\",\n        \"hostId\": \"872e9b93ccd50b93612d49f4c371f49fe6365f02bcb51994868eb88f\",\n        \"OS-EXT-SRV-ATTR:host\": \"controller\",\n        \"addresses\": {\n            \"officenet\": [\n                {\n                    \"OS-EXT-IPS-MAC:mac_addr\": \"fa:16:3e:25:0d:c8\",\n                    \"version\": 4,\n                    \"addr\": \"10.43.1.183\",\n                    \"OS-EXT-IPS:type\": \"fixed\"\n                }\n            ]\n        },\n        \"links\": [\n            {\n                \"href\": \"http://10.43.1.197:8774/v2/1333f00f05564a02b3c4f608b9622fc9/servers/4e357d2e-b3b1-4160-95b9-fc9102a375d9\",\n                \"rel\": \"self\"\n            },\n            {\n                \"href\": \"http://10.43.1.197:8774/1333f00f05564a02b3c4f608b9622fc9/servers/4e357d2e-b3b1-4160-95b9-fc9102a375d9\",\n                \"rel\": \"bookmark\"\n            }\n        ],\n        \"key_name\": null,\n        \"image\": {\n            \"id\": \"b724ae53-9113-4e33-987a-f58fd9a4ec81\",\n            \"links\": [\n                {\n                    \"href\": \"http://10.43.1.197:8774/1333f00f05564a02b3c4f608b9622fc9/images/b724ae53-9113-4e33-987a-f58fd9a4ec81\",\n                    \"rel\": \"bookmark\"\n                }\n            ]\n        },\n        \"OS-EXT-STS:task_state\": null,\n        \"OS-EXT-STS:vm_state\": \"active\",\n        \"OS-EXT-SRV-ATTR:instance_name\": \"instance-00000017\",\n        \"OS-SRV-USG:launched_at\": \"2014-07-11T11:43:41.000000\",\n        \"OS-EXT-SRV-ATTR:hypervisor_hostname\": \"controller\",\n        \"flavor\": {\n            \"id\": \"2\",\n            \"links\": [\n                {\n                    \"href\": \"http://10.43.1.197:8774/1333f00f05564a02b3c4f608b9622fc9/flavors/2\",\n                    \"rel\": \"bookmark\"\n                }\n            ]\n        },\n        \"id\": \"4e357d2e-b3b1-4160-95b9-fc9102a375d9\",\n        \"security_groups\": [\n            {\n                \"name\": \"default\"\n            }\n        ],\n        \"OS-SRV-USG:terminated_at\": null,\n        \"OS-EXT-AZ:availability_zone\": \"nova\",\n        \"user_id\": \"a859477b42794e8fa6dbf339361bb268\",\n        \"name\": \"Akshay-Debian-2\",\n        \"created\": \"2014-07-11T11:43:35Z\",\n        \"tenant_id\": \"1333f00f05564a02b3c4f608b9622fc9\",\n        \"OS-DCF:diskConfig\": \"MANUAL\",\n        \"os-extended-volumes:volumes_attached\": [],\n        \"accessIPv4\": \"\",\n        \"accessIPv6\": \"\",\n        \"progress\": 0,\n        \"OS-EXT-STS:power_state\": 1,\n        \"config_drive\": \"\",\n        \"metadata\": {}\n    }\n}";

        try {
            Gson gson = new Gson();
            Server server = gson.fromJson( myJSONString, Server.class); 
            System.out.println(server.getName());         
            System.out.println(server.getHostId());
            System.out.println(server.getId());
            System.out.println(server.getTenantId());
            System.out.println(server.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }                   
    }
}

我打印的getId等值是NULL,我不明白我在这里做错了什么? 请告诉我。这样编译时没有错误,也没有警告。

0 个答案:

没有答案