DeserializeObject <t>返回具有空值/默认值的对象

时间:2018-07-14 22:05:08

标签: c# json.net

如果我尝试反序列化以下json

  

  #ticker-banner.hd-tickerloop {
    overflow:hidden;
    width:100%
    }

    .header-banner .header-promotion {
    background-color:#2abad9;
    color:#fff;
    padding:16px 0;
    text-align:center;
    font-size:.875rem;
    letter-spacing:.0375rem
    }
    #ticker-banner.hd-tickerloop {
    overflow:hidden;
    width:100%
    }

    #ticker-banner.hd-tickerloop2 .hd-loop-container {
    display:flex;
    align-items:center;
    width:200%;
    -webkit-animation:tickerloop2 20s infinite;
    animation:tickerloop2 15s infinite;
    animation-duration:15s
    }

    #ticker-banner.hd-tickerloop2 .hd-loop-container:hover,#ticker-banner.hd- 
     tickerloop2 .hd-loop-container:active,#ticker-banner.hd-tickerloop2 .hd- 
     loop- 
     container:focus-within {
    -webkit-animation-play-state:paused;
    -moz-animation-play-state:paused;
    -o-animation-play-state:paused;
    animation-play-state:paused
    }

    #ticker-banner.hd-tickerloop2 .ticker-item {
    width:75%;
    }
    .ticker-item {
    font-size:12px;
    line-height:1.4em;
    color:#fff;
    text-decoration:none
    }

    #ticker-banner.hd-tickerloop2 .ticker-item {
    -moz-box-sizing:border-box;
    box-sizing:border-box;
    padding:0 2em
    }

   @keyframes tickerloop2 {
          0% { transform:translateX(0);    flex-direction:row; }
         40% { transform:translateX(0);    flex-direction:row; }
         50% { transform:translateX(-50%); flex-direction:row; }
    89.9999% { transform:translateX(-50%); flex-direction:row; }
         90% { transform:translateX(0);    flex-direction:row-reverse; }
    99.9999% { transform:translateX(-50%); flex-direction:row-reverse; }
        100% { transform:translateX(0);    flex-direction:row; }
    }

我希望将其反序列化为结构完全不同的类时会抛出异常

<div class="header-banner">
    <div class="header-promotion">
        <div id="ticker-banner" class="hd-tickerloop hd-tickerloop2">
            <div class="hd-loop-container">
               <div id="ticker-item1" class="ticker-item" tabindex="0">Do You Want To Save 10%? Click Here To Subscribe + Save</div>
               <div id="ticker-item2" class="ticker-item" tabindex="0">Receive Free Shipping On All U.S. Orders of $65 Or More</div>
               <div id="ticker-item3" class="ticker-item" tabindex="0">Clean Beauty. Because Girl, You're Too Pretty To Posion™</div>
               
            </div>
        </div>
    </div>
</div>

但是它返回一个带有默认/空值的对象

{ "error": "Invalid order request" }

我的OrderResponse类如下:

var response = JsonConvert.DeserializeObject<OrderResponse>(errorJson);

我希望反序列化步骤引发异常或返回空对象。我以为添加[JsonConstructor]属性可以解决问题,但此处没有问题。

我做错什么了吗?我需要创建自己的JsonConverter还是可以修改其他一些de / serializer设置?

1 个答案:

答案 0 :(得分:4)

要抛出异常,您将必须更改默认的序列化设置。默认情况下,当类中缺少Json成员时,Json.NET将忽略:

  

MissingMemberHandling –默认情况下,此属性设置为“忽略”   这意味着,如果Json具有的属性在   目标对象,它只会被忽略。将此设置为错误将导致   如果json包含不包含的成员,则抛出一个异常   存在于目标对象类型上。

代码应如下所示:

JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
serializerSettings.MissingMemberHandling = MissingMemberHandling.Error;
var response = JsonConvert.DeserializeObject<OrderResponse>(errorJson, serializerSettings);