Home > Mobile >  How to remove "Double braces {{}}" in a property of dynamic type during deserialization (A
How to remove "Double braces {{}}" in a property of dynamic type during deserialization (A

Time:11-27

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?

CodePudding user response:

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 = JsonConvert.DeserializeObject<JObject>(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);

CodePudding user response:

You have to fix the code that returns the double braces string. If you don't have access to this code, the only way to get rid of double braces is to use string function

storedEvent.Data=storedEvent.Data.Substring(1,Data.Length-2);

after this you can use your code if you like, but I don't like dynamic, IMHO it is better to use Parse and sqware brakets to get data.

  • Related