如何将此json转换为POJO结构

时间:2016-06-04 18:05:13

标签: java android json pojo gson

我的Android应用程序的JSON API结果如下所示(缩小到大小)

{

"Results": [{
    "data": [{
        "AAA": {
            "accessRole": "access.role.owner",
            "fullName": "jsddd",
            "spaceTotal": null,
            "createdDT": "2016-05-29T02:52:11.000000Z",
            "updatedDT": "2016-05-29T02:52:11.000000Z"
        }
    },
    {
        "AAA": {
            "accessRole": "access.role.owner",
            "fullName": "jsw",
            "createdDT": "2016-05-29T04:48:57.000000Z",
            "updatedDT": "2016-05-29T04:48:57.000000Z"
        }
    },
    {
        "BBB": {
            "archiveId": 1,
            "description": "the description here",
            "createdDT": "2016-05-29T02:52:11.000000Z",
            "updatedDT": "2016-06-01T22:49:01.000000Z"
        }
    }],
    "message": ["Get successful"],
    "status": true,
    "createdDT": null,
    "updatedDT": null
}],
"isSuccessful": true,
"createdDT": null,
"updatedDT": null

}

我希望在我的deserialize函数中将结构转换为POJO(如下所示),这是通过jsonschema2pojo.org运行它然后能够在其上使用GSON

{

"Results": [{
    "data": [{
        "AAAs": [{
            "accessRole": "access.role.owner",
            "fullName": "jsddd",
            "spaceTotal": null,
            "createdDT": "2016-05-29T02:52:11.000000Z",
            "updatedDT": "2016-05-29T02:52:11.000000Z"
        },
        {
            "accessRole": "access.role.owner",
            "fullName": "jsw",
            "createdDT": "2016-05-29T04:48:57.000000Z",
            "updatedDT": "2016-05-29T04:48:57.000000Z"
        }],
        "BBBs": [{
            "archiveId": 1,
            "description": "the description here",
            "createdDT": "2016-05-29T02:52:11.000000Z",
            "updatedDT": "2016-06-01T22:49:01.000000Z"
        }]
    }],
    "message": ["Get successful"],
    "status": true,
    "createdDT": null,
    "updatedDT": null
}],
"isSuccessful": true,
"createdDT": null,
"updatedDT": null

}

想知道是否有办法优雅地做到这一点,而不是通过逐个阅读对象并从头开始重建来进行暴力破解。

如果可以完成,则无需在数据元素(AAA,BBB)中明确编码,因为它们将随着时间的推移而增长。

编辑: org.jsonschema2pojo创建的输出不匹配就是将元素作为单个对象而不是数组。

package com.example;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;

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

    private AAA aAA;
    private BBB bBB;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public AAA getAAA() {
        return aAA;
    }

    public void setAAA(AAA aAA) {
        this.aAA = aAA;
    }

    public BBB getBBB() {
        return bBB;
    }

    public void setBBB(BBB bBB) {
        this.bBB = bBB;
    }

    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

0 个答案:

没有答案