I have the following code:
return SurveyKit(
onResult: (SurveyResult result) {
print(result.finishReason);
},
...
);
Do you know how to serialize result.results
to a JSON file?
Thank you.
CodePudding user response:
The code below would do the trick. It converts QueryResult questions and answers to JSON by iterating on every question and then every answer.
Map<String, dynamic> queryResultToJson(SurveyResult result) {
return <String, dynamic>{
"finishReason": result.finishReason.name,
"steps": result.results
.map((step) => <String, dynamic>{
"id": step.id?.id,
"startDate": step.startDate.toIso8601String(),
"endDate": step.endDate.toIso8601String(),
"results": step.results
.map((r) => <String, dynamic>{
"id": r.id?.id,
"result": r.result is BooleanResult
? ((r.result as BooleanResult) == BooleanResult.POSITIVE
? true
: (r.result as BooleanResult) == BooleanResult.NEGATIVE
? false
: null)
: r.result is TimeOfDay
? '${(r.result as TimeOfDay).hour}:${(r.result as TimeOfDay).minute}'
: r.result is DateTime
? (r.result as DateTime).toIso8601String()
: r.result,
"startDate": r.startDate.toIso8601String(),
"endDate": r.endDate.toIso8601String(),
"valueIdentifier": r.valueIdentifier,
})
.toList()
})
.toList()
};
}
And then, to convert it to String
:
final json = queryResultToJson(result);
print(jsonEncode(json));
This is the result of running the SurveyKit example code
{
"finishReason": "COMPLETED",
"steps": [
{
"id": "1",
"startDate": "2022-06-28T16:23:45.752352",
"endDate": "2022-06-28T16:23:47.360266",
"results": [
{
"id": "1",
"result": null,
"startDate": "2022-06-28T16:23:45.752352",
"endDate": "2022-06-28T16:23:47.360266",
"valueIdentifier": "instruction"
}
]
},
{
"id": "2",
"startDate": "2022-06-28T16:23:47.420516",
"endDate": "2022-06-28T16:23:50.420628",
"results": [
{
"id": "2",
"result": 24,
"startDate": "2022-06-28T16:23:47.420516",
"endDate": "2022-06-28T16:23:50.420628",
"valueIdentifier": "24"
}
]
},
{
"id": "3",
"startDate": "2022-06-28T16:23:50.451292",
"endDate": "2022-06-28T16:24:02.388185",
"results": [
{
"id": "3",
"result": true,
"startDate": "2022-06-28T16:23:50.451292",
"endDate": "2022-06-28T16:24:02.388185",
"valueIdentifier": "Yes"
}
]
},
{
"id": "4",
"startDate": "2022-06-28T16:24:02.413570",
"endDate": "2022-06-28T16:24:05.266691",
"results": [
{
"id": "4",
"result": "asdf",
"startDate": "2022-06-28T16:24:02.413570",
"endDate": "2022-06-28T16:24:05.266691",
"valueIdentifier": "asdf"
}
]
},
{
"id": "5",
"startDate": "2022-06-28T16:24:05.286859",
"endDate": "2022-06-28T16:24:06.176940",
"results": [
{
"id": "5",
"result": 3.0,
"startDate": "2022-06-28T16:24:05.286859",
"endDate": "2022-06-28T16:24:06.176940",
"valueIdentifier": "3.0"
}
]
},
{
"id": "6",
"startDate": "2022-06-28T16:24:06.190944",
"endDate": "2022-06-28T16:24:08.361066",
"results": [
{
"id": "6",
"result": [
{
"text": "Pet",
"value": "Pet"
}
],
"startDate": "2022-06-28T16:24:06.190944",
"endDate": "2022-06-28T16:24:08.361066",
"valueIdentifier": "Pet"
}
]
},
{
"id": "7",
"startDate": "2022-06-28T16:24:08.381213",
"endDate": "2022-06-28T16:24:09.835139",
"results": [
{
"id": "7",
"result": {
"text": "No",
"value": "No"
},
"startDate": "2022-06-28T16:24:08.381213",
"endDate": "2022-06-28T16:24:09.835139",
"valueIdentifier": "No"
}
]
},
{
"id": "8",
"startDate": "2022-06-28T16:24:09.861336",
"endDate": "2022-06-28T16:24:10.975047",
"results": [
{
"id": "8",
"result": "12:0",
"startDate": "2022-06-28T16:24:09.861336",
"endDate": "2022-06-28T16:24:10.975047",
"valueIdentifier": "TimeOfDay(12:00)"
}
]
},
{
"id": "9",
"startDate": "2022-06-28T16:24:11.006271",
"endDate": "2022-06-28T16:24:11.933636",
"results": [
{
"id": "9",
"result": "2021-06-25T04:08:16.000Z",
"startDate": "2022-06-28T16:24:11.006271",
"endDate": "2022-06-28T16:24:11.933636",
"valueIdentifier": "2021-06-25T04:08:16.000Z"
}
]
},
{
"id": "10",
"startDate": "2022-06-28T16:23:45.753350",
"endDate": "2022-06-28T16:24:14.027346",
"results": [
{
"id": "10",
"result": null,
"startDate": "2022-06-28T16:23:45.753350",
"endDate": "2022-06-28T16:24:14.027346",
"valueIdentifier": "completion"
}
]
}
]
}