Page 1 of 1

Decode json string to a struct

Posted: Wed Nov 18, 2020 9:59 am
by mmarchha
I have a struct with a few string data members. Encoding this struct to a json string works perfectly fine with jsonEncode().
But when I try to convert this json string back to the struct using jsonDecode I get a warning message that the returned data type does not match my struct data type.

WARNING, 28/ctrl, mapping, Übergebener Datentyp entspricht nicht dem benötigten Typ 'BeccInfoWindowEntry',

I think the problem is that the jsonDecode() returns just a mapping and this, of course, can not be assigned to the struct.

Is there a way to tell jsonDecode() it should decode into and return the struct?

Re: Decode json string to a struct

Posted: Fri Nov 20, 2020 1:08 pm
by gschijndel
There is no way to tell 'jsonDecode' to return the struct. I would extend the struct with a static 'fromJson' function to deserialize the json string and return a new struct with the data from the json string.