ASPNET 5 vNext Json序列化

时间:2015-10-24 17:06:35

标签: json.net asp.net-core

我正在使用ASP8 vNext的beta8版本,而JSON序列化无效。我看不到会导致序列化失败的任何事情,但也许我在startup.cs中缺少一些JsonOptions?

我的班级定义如下:

public class Bcp
{
    public string businessObjectives{get; set;}
    public string businessOwners{get; set;}
    public BcpCountry country{get;set;}
    public List<BcpPartner> businessPartners{get; set;}
}

public class BcpCountry{
    public string name{get; set;}
    public string code{get; set;}
}

public class BcpPartner{
    public string BusinessPartner{get; set;}
    public string BusinessPartnerRelationship{get; set;}
}

如果json有效负载具有null businessPartners属性,那么所有序列化都很好,但如果属性有任何内容(甚至是空数组),那么该对象为空。

正确地序列化到班级: { "businessObjectives":"", "businessOwners":"Owner", "country":{"name":"Åland Islands","code":"AX"}, "businessPartners":null } 不序列化和对象为空: { "businessObjectives":"", "businessOwners":"Owner", "country":{"name":"Åland Islands","code":"AX"}, "businessPartners":[{"BusinessPartner":"PARTNER 1","BusinessPartnerRelationship":"rel 1"}] }

这是一个简单的控制器动作:

 [HttpPost("{id}")]
 public IActionResult Update(string id, [FromBody] Bcp data){
     string error = null;
     _logger.LogInformation("Saving " + id);

     var json = JsonConvert.SerializeObject(data);
     _logger.LogInformation(json);
 }

0 个答案:

没有答案