1

I have the following JSON returned by a REST API call. Is it even possible to read this construct into a Java object? here is the JSON data:

[
    {
        "dirName": "/",
        "files": [
            {
                "fileName": "Dog prototype",
                "fileId": "0a8cc3ed206",
                "revision": {
                    "revisedBy": "Billy Jean",
                    "imageId": "80e94300a7286"
                },
                "creatorName": "Billy Jean",
                "disciplineName": "Structural Engineering",
                "projectName": "Army Dog Reengineering Project",
                "directoryId": "ed8202c43332"
            },
            {
                "fileName": "Office space plan",
                "fileId": "e5c4ceb41e9b",
                "revision": {
                    "revisedBy": "Winslow Homer",
                    "imageId": ""
                },
                "creatorName": "Winslow Homer",
                "disciplineName": "Interior Planning",
                "projectName": "Friggle LLC Office Redesign",
                "directoryId": "ed85a61202c43332"
            }
        ],
        "dirId": "ed85a66f08cd49332"
    }
]

Can Gson work with something like this?

Thanks

1 Answer 1

6

You need to mimic the structure of the data and then the gson library will take care of everything, just make sure they match.

class Directory {
  String dirName;
  File[] files;
  String dirId;
}

class File {
  String fileName;
  String fileId;
  Revision revision;
  String creatorName;
  String disciplineName;
  String projectName;
  String directoryId
}

class Revision {
  String revisedBy;
  String imageId;
}

Directory[] directories = gson.fromJson(data, Directory[].class);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.