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);
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.
Doing this:
// JSON
{
"intlDateTime": "On the {{val, datetime}}",
}
i18next.t('intlDateTime', { val: new Date(Date.UTC(2012, 11, 20, 3, 0, 0)) });
// --> On the 12/20/2012
without interpolation escape to false
ends with a display of / instead of "/"
How can we use date format and avoid xss attacks at the same time ?
To solve the wrong display, I need to write:
{t('lastModified', {
val: new Date(Date.UTC(2012, 11, 20, 3, 0, 0)),
interpolation: { escapeValue: false },
})}
which I would like to avoid.
I have a result array from laravel8 relations. like below
$val = {
"q_id": 1,
"q_text": "1111",
"q_mandatory": 1,
"q_status": "unpublished",
"que_logic_relation": [
{
"ql_id": 1,
"ql_quest_id": 1,
"ql_answer_choice_id": null,
"ql_succeeding_q_order": 3,
},
{
"ql_id": 4,
"ql_quest_id": 1,
"ql_answer_choice_id": null,
"ql_succeeding_q_order": 3,
}
]
}
When I print $val['q_text'] // output 1111
when I print $val['que_logic_relation'] //no result or empty
I want to print the data in que_logic_relation seperately. How can I do that?
Issued fixed by adding toArray() to the query result and everything works fine now.
I am using laravel 5.2 and I am a little confused.
How i can get value for nama_kategori and nama_wil ?
it's more detail code http://pastebin.com/HXncDnDT
I have a data array like this:
[
{
"id_rate_kend": 1,
"kategori": {
"kat_id": 1,
"id_jns_kend": 1,
"nama_kategori": "Kategori 1",
"jenis_kendaraan": {
"id_jns_kend": 1,
"keterangan": "Non Bus dan Non Truk"
}
},
"wilayah": {
"id_wil": 1,
"nama_wil": "Wilayah 1"
},
"rate_master": {
"id_rate_mst": 5,
"type_rate": "1",
"keterangan": "",
"tenor_type": "tahun",
"is_kombinasi": "N",
"permil": "Y"
}
},
]
How do i get value for nama_kategori? wilayah?
Im trying to understand if there is a feature in ractivejs , for descending sorting , and ascending .
I couldnt find anyhting in the documentation .
No - Ractive purposely avoids being a 'kitchen sink' utility library. But it's very easy to add an ascending or descending helper:
var helpers = Ractive.defaults.data;
// assuming a and b are numbers...
helpers.ascending = function ( a, b ) {
return a - b;
};
helpers.descending = function ( a, b ) {
return b - a;
};
ractive = new Ractive({
el: 'body',
template: '' +
'<p>ascending: {{ numbers.slice().sort(ascending) }}</p>' +
'<p>descending: {{ numbers.slice().sort(descending) }}</p>'
},
data: {
numbers: [ 9, 4, 6, 2, 4, 1, 10, 2, 7, 8 ]
}
});
Note that you could also put the ascending and descending functions directly on the data object, if that's preferable.
Here's a JSFiddle to demonstrate: http://jsfiddle.net/rich_harris/nszt3150/