我有一个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可以使用这样的东西吗?
由于
答案 0 :(得分: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);