10

I'm trying use the Box 2.0 API which returns everything in JSON. This is great because I can use JSON.deserialize. I've run into one issue when getting the folder and file structure. Box returns it as an array of objects that can contain both files and folders. I'd like to have different Apex classes that represent files and folders, but can't figure out if it is possible to deserialize the JSON into these two different classes. The other option I have is to create a single class that is a superset of all fields. Here's a snippet of the JSON.

{"item_collection": {
    "total_count": 2,
    "entries": [
        {
            "type": "folder",
            "id": "344470651",
            "sequence_id": "0",
            "etag": "0",
            "name": "Signed and returned by you"
        },
        {
            "type": "file",
            "id": "2778062467",
            "sequence_id": "0",
            "etag": "0",
            "sha1": "92da54134a52f3e264ab9ed346b3be95c9b2cdad",
            "name": "Welcome.pdf"
        }
    ]
}

Am I stuck doing the superset class or is there a way to get them split into separate classes?

7

Using the http://json2apex.herokuapp.com app developed by metadady and superfell was able to parse your JSON into apex class

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

    public Item_collection item_collection;

    public class Item_collection {
        public Integer total_count;
        public List<Entries> entries;
    }

    public class Entries {
        public String type;
        public String id;
        public String sequence_id;
        public String etag;
        public String name;
        public String sha1;
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }

    static testMethod void testParse() {
        String json = '{\"item_collection\": {'+
        '    \"total_count\": 2,'+
        '    \"entries\": ['+
        '        {'+
        '            \"type\": \"folder\",'+
        '            \"id\": \"344470651\",'+
        '            \"sequence_id\": \"0\",'+
        '            \"etag\": \"0\",'+
        '            \"name\": \"Signed and returned by you\"'+
        '        },'+
        '        {'+
        '            \"type\": \"file\",'+
        '            \"id\": \"2778062467\",'+
        '            \"sequence_id\": \"0\",'+
        '            \"etag\": \"0\",'+
        '            \"sha1\": \"92da54134a52f3e264ab9ed346b3be95c9b2cdad\",'+
        '            \"name\": \"Welcome.pdf\"'+
        '        }'+
        '    ]'+
        '}'+
        '}';
        JSON2Apex obj = parse(json);
        System.assert(obj != null);
    }
}

Please try and it does magic .Thanks to metadady and Superfell and whoever built this app on herouku platform

Once we have the List of Entries(List entries)you can separated by using the Type in an another list .

1
  • Yes, this is what I've done with the superset class. I was hoping for something a little more elegant. There's the potential that there are a lot Entries so looping through them to separate into Files and Folders is pretty resource intensive. – Daniel Hoechst Dec 14 '12 at 15:29
-1

@Mohith Shrivastava, do you have the code of loop through item_colletion and entries to get the data after JSON2Apex obj = parse(json)?

Thanks in advance,

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.