Welcome to OGeek Q&A 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...