我需要将“内容”数组的每个元素转换为对象。
这是json:
{
"content": [
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"author": {
"userId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
},
"createDate": "2020-01-30T20:18:29.764Z",
"executor": {
"userId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
},
}
],
"first": true,
"numberOfElements": 0,
}
问题在于“内容”数组位于json中,并且它作为执行者和作者的部分也必须是对象,而我不知道该如何到达并进行解析。
怎么做?任何帮助,谢谢。
最佳答案
您可以访问以下元素:
var decoded = json.decode(j);
var inner = decoded['content'][0]; // if you expect more than one entry, iterate the list
print(inner['id']); // -> 3fa85f64-5717-4562-b3fc-2c963f66afa6
print(inner['createDate']);
print(inner['author'].runtimeType); // another Map<String, dynamic> as expected
print(inner['author']['userId']);
您可以根据需要创建Dart类来建模,例如,“用户”。
关于flutter - 如何将内部json部分转换为dart/flutter中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59994801/