Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

c# - Remove double curly brackets from JObject that have been added during deserialization

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...