When deserializing a string, the curly braces are duplicating and this is disturbing the reading of some fields. I've tried everything, but I can't serialize correctly, without duplicate curly braces.
I already tried to do it like this:
dynamic values = JsonConvert.DeserializeObject<dynamic>(storedEvent.Data);
The storedEvent.Data property is of type string and contains this information:
"{\"PaisId\":31,\"PaisDivisaoAdministrativaNivelRemovedEventList\":[{\"Id\":6,\"PaisId\":31,\"PaisNomePtBr\":\"\",\"PaisDivisaoAdministrativaTipoId\":5,\"PaisDivisaoAdministrativaTipoNome\":\"Município\",\"PaisDivisaoAdministrativaTipoOrigemId\":5,\"Timestamp\":\"2022-11-24T20:16:15.6020289-03:00\",\"MessageType\":\"PaisDivisaoAdministrativaNivelRemovedEvent\",\"AggregateId\":6},{\"Id\":5,\"PaisId\":31,\"PaisNomePtBr\":\"\",\"PaisDivisaoAdministrativaTipoId\":1,\"PaisDivisaoAdministrativaTipoNome\":\"Estado\",\"PaisDivisaoAdministrativaTipoOrigemId\":null,\"Timestamp\":\"2022-11-24T20:16:15.6580242-03:00\",\"MessageType\":\"PaisDivisaoAdministrativaNivelRemovedEvent\",\"AggregateId\":5}],\"Timestamp\":\"2022-11-24T20:16:16.1892039-03:00\",\"MessageType\":\"PaisDivisaoAdministrativaNivelHierarquiasRemovedEvent\",\"AggregateId\":31}"
Result obtained (Duplicate curly braces):
{{
"PaisId": 31,
"PaisDivisaoAdministrativaNivelRemovedEventList": [
{
"Id": 6,
"PaisId": 31,
"PaisNomePtBr": "",
"PaisDivisaoAdministrativaTipoId": 5,
"PaisDivisaoAdministrativaTipoNome": "Município",
"PaisDivisaoAdministrativaTipoOrigemId": 5,
"Timestamp": "2022-11-24T20:16:15.6020289-03:00",
"MessageType": "PaisDivisaoAdministrativaNivelRemovedEvent",
"AggregateId": 6
},
{
"Id": 5,
"PaisId": 31,
"PaisNomePtBr": "",
"PaisDivisaoAdministrativaTipoId": 1,
"PaisDivisaoAdministrativaTipoNome": "Estado",
"PaisDivisaoAdministrativaTipoOrigemId": null,
"Timestamp": "2022-11-24T20:16:15.6580242-03:00",
"MessageType": "PaisDivisaoAdministrativaNivelRemovedEvent",
"AggregateId": 5
}
],
"Timestamp": "2022-11-24T20:16:16.1892039-03:00",
"MessageType": "PaisDivisaoAdministrativaNivelHierarquiasRemovedEvent",
"AggregateId": 31
}}
Expected:
{
"PaisId": 31,
"PaisDivisaoAdministrativaNivelRemovedEventList": [
{
"Id": 6,
"PaisId": 31,
"PaisNomePtBr": "",
"PaisDivisaoAdministrativaTipoId": 5,
"PaisDivisaoAdministrativaTipoNome": "Município",
"PaisDivisaoAdministrativaTipoOrigemId": 5,
"Timestamp": "2022-11-24T20:16:15.6020289-03:00",
"MessageType": "PaisDivisaoAdministrativaNivelRemovedEvent",
"AggregateId": 6
},
{
"Id": 5,
"PaisId": 31,
"PaisNomePtBr": "",
"PaisDivisaoAdministrativaTipoId": 1,
"PaisDivisaoAdministrativaTipoNome": "Estado",
"PaisDivisaoAdministrativaTipoOrigemId": null,
"Timestamp": "2022-11-24T20:16:15.6580242-03:00",
"MessageType": "PaisDivisaoAdministrativaNivelRemovedEvent",
"AggregateId": 5
}
],
"Timestamp": "2022-11-24T20:16:16.1892039-03:00",
"MessageType": "PaisDivisaoAdministrativaNivelHierarquiasRemovedEvent",
"AggregateId": 31
}
Does anyone know of a solution?
Using dynamic
is not a good option to work with Json objects
dynamically
For this, you can use Jobject
as follows
using Newtonsoft.Json.Linq;
string jsonStr= "{\"PaisId\":31,\"PaisDivisaoAdministrativaNivelRemovedEventList\":[{\"Id\":6,\"PaisId\":31,\"PaisNomePtBr\":\"\",\"PaisDivisaoAdministrativaTipoId\":5,\"PaisDivisaoAdministrativaTipoNome\":\"Município\",\"PaisDivisaoAdministrativaTipoOrigemId\":5,\"Timestamp\":\"2022-11-24T20:16:15.6020289-03:00\",\"MessageType\":\"PaisDivisaoAdministrativaNivelRemovedEvent\",\"AggregateId\":6},{\"Id\":5,\"PaisId\":31,\"PaisNomePtBr\":\"\",\"PaisDivisaoAdministrativaTipoId\":1,\"PaisDivisaoAdministrativaTipoNome\":\"Estado\",\"PaisDivisaoAdministrativaTipoOrigemId\":null,\"Timestamp\":\"2022-11-24T20:16:15.6580242-03:00\",\"MessageType\":\"PaisDivisaoAdministrativaNivelRemovedEvent\",\"AggregateId\":5}],\"Timestamp\":\"2022-11-24T20:16:16.1892039-03:00\",\"MessageType\":\"PaisDivisaoAdministrativaNivelHierarquiasRemovedEvent\",\"AggregateId\":31}";
var obj = JObject.Parse(jsonStr);
//access properties
var paisId = obj["PaisId"];
//Access to PaisDivisaoAdministrativaNivelRemovedEventList
var paisList = obj["PaisDivisaoAdministrativaNivelRemovedEventList"].ToList();
//search on PaisDivisaoAdministrativaNivelRemovedEventList
var result = paisList.FirstOrDefault(x => (int)x["Id"] == 6);