0

I have a JSON API result to my android app that looks like the following (cut down to size)

{

"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

}

and am looking to convert the structure into something that is POJO (shown below) in my deserialize function as was defined by running this through jsonschema2pojo.org and then be able to use GSON on it

{

"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

}

Was wondering if there is a way to do this elegantly instead of brute forcing by reading in the objects one by one and rebuilding it from scratch.

Bonus if it could be done w/o having to explicitly code in the data elements (AAA, BBB) as they will be growing in number over time.

Edited: The output that org.jsonschema2pojo is creating that doesn't match is in it making the elements as a single object not as as array.

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

}
Community
  • 1
  • 1
Jim
  • 136
  • 1
  • 4
  • 1
    http://www.jsonschema2pojo.org/ – USKMobility Jun 04 '16 at 18:08
  • Pardon my confusion, but what you say is POJO is simply the JSON again. Did you actually run it through jsonschema2pojo.org? – Daniel Jun 04 '16 at 18:13
  • The top structure doesn't conform to POJO standards because it tries to make them into Objects not an Array. The problem is more evident when I try to send a response back to the server and I have several of the same item, like when i do a delete. jsonschema2pojo.org is giving me a structure that only allows one item not multiple. – Jim Jun 05 '16 at 06:07

0 Answers0