1

我有以下由 REST API 调用返回的 JSON。甚至可以将此构造读入 Java 对象吗?这是 JSON 数据:

[
    {
        "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"
    }
]

Gson 可以使用这样的东西吗?

谢谢

4

1 回答 1

6

您需要模仿数据的结构,然后 gson 库会处理所有事情,只要确保它们匹配即可。

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);
于 2013-05-16T03:39:42.060 回答