I have a JSON string that starts and ends with curly brackets "{}".
I then deserialize the object but when this is done I see that I now have double curly brackets at the start and the end "{{}}".
My code looks something like this
//deserializeobject json string into jobject JObject loanVersionedDoc = JsonConvert.DeserializeObject<JObject>(s); //Get the latest value from versioned document JObject loanLatestVersion = Versioning.demultiplicifyingParseForLatest(loanVersionedDoc); //TODO get the latest activity.isComplete value string activityCompletionStatus = (string)loanVersionedDoc.GetValue("Activities[0].isComplete");
This is what my JSON string looks like
"{ \"_id\" : \"582c459d54b6e43d307929f8\", \"LoanName\" : ... }
This is what my loanVersionedDoc looks like
{{ "LoanName": "Test One", "isActive": "True", "Stages": [ { "StageName": "Stage One", "isComplete": false }, { "StageName": "Stage Two - CAG Approval and Indicative Terms", "isComplete": true }, { "StageName": "Stage Three", "isComplete": false } ], "Activities": [ { "ActivityName": "Generate due diligence report", "isComplete": "Complete", "ActivityParent": "Stage Two - CAG Approval and Indicative Terms" }, { "ActivityName": "Received Stage 2 document from BDM", "isComplete": "NA", "ActivityParent": "Stage Two - CAG Approval and Indicative Terms" }, ... }}
What must I be doing wrong to cause the JObject to inherit an extra curly bracket when deserialized?
Is this causing a problem or are you just curious? I had the same issue when I was sending data as the type "object" inside another a container class. The container itself was being deserialized properly but the object inside wasn't. I thought it wasn't deserializing it because of the double curly braces. In reality, it seems that may just be how JObjects look. The real reason was probably because I had turned off the setting where it sent the type information and since I was deserializing to "object" it couldn't possibly know what the type from a string alone.
Either way, I noticed if you did ".ToString()" on it then the double curly braces would disappear. This meant I was able to solve my issue by simply doing:
var someType = JsonConvert.DeserializeObject<SomeType>(jObject.ToString());
I'm not sure if this is a bug or not but my guess is that it's simply an internal implementation detail and that's why they have it 'fixed' when you ".ToString()".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With