从反序列化过程中添加的JObject删除双大括号 [英] Remove double curly brackets from JObject that have been added during deserialization

查看:1280
本文介绍了从反序列化过程中添加的JObject删除双大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以大括号"{}"开头和结尾的JSON字符串.

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 "{{}}".

我的代码看起来像这样

    //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");

这是我的JSON字符串的样子

This is what my JSON string looks like

"{ \"_id\" : \"582c459d54b6e43d307929f8\", \"LoanName\" :
...
 }

这是我的loanVersionedDoc的样子

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"
    },
...
}}

在反序列化时,导致JObject继承一个额外的花括号,我该怎么办?

What must I be doing wrong to cause the JObject to inherit an extra curly bracket when deserialized?

推荐答案

这是引起问题还是您只是好奇?当我在另一个容器类中发送数据作为对象"类型时,我遇到了同样的问题.容器本身已正确反序列化,但内部对象未正确反序列化.我以为它没有反序列化,因为双花括号.实际上,似乎只是JObjects的外观.真正的原因可能是因为我已经关闭了它发送类型信息的设置,并且由于我反序列化为对象",所以无法单独从字符串中知道什么类型.

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.

无论哪种方式,我都注意到,如果您对它执行了".ToString()",则双花括号将消失.这意味着我只需执行以下操作即可解决我的问题:

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());

我不确定这是否是一个错误,但我想这只是一个内部实现细节,这就是为什么当您".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()".

这篇关于从反序列化过程中添加的JObject删除双大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆