便携式库错误

问题描述:

我有一个的WebAPI返回以下JSON而我试图反序列化对象下面便携式库错误

JSON对象

{ 
    "results": [{ 
      "id": 123456, 
      "fullName": "Foo Bar", 
      "localName": null, 
      "jobPosition": "ME", 
      "jobCompanyName": "EXTRA", 
      "jobLocationCountry": "United States of America", 
      "jobLocationCity": "San Francisco", 
      "jobCountrySubdivision": "California", 
      "boards": [], 
      "restrictionsIndicator": false, 
      "personRestriction": null, 
      "jobRestriction": null 
     }, { 
      "id": 789101, 
      "fullName": "Foo Bar", 
      "localName": null, 
      "jobPosition": null, 
      "jobCompanyName": "Unknown", 
      "jobLocationCountry": "Unknown", 
      "jobLocationCity": "Unknown", 
      "jobCountrySubdivision": "Unknown", 
      "boards": [{ 
        "companyId": 667525, 
        "companyName": "FOO BAR COMPANY", 
        "companyOffLimits": null, 
        "restrictionCategoryId": null 
       } 
      ], 
      "restrictionsIndicator": false, 
      "personRestriction": null, 
      "jobRestriction": null 
     } 
    ], 
    "totalCount": 2, 
    "pageNumber": 1, 
    "resultsPerPage": 100 
} 

C#类

public class Rootobject 
{ 
    public Result[] results { get; set; } 
    public int totalCount { get; set; } 
    public int pageNumber { get; set; } 
    public int resultsPerPage { get; set; } 
} 

public class Result 
{ 
    public int id { get; set; } 
    public string fullName { get; set; } 
    public object localName { get; set; } 
    public string jobPosition { get; set; } 
    public string jobCompanyName { get; set; } 
    public string jobLocationCountry { get; set; } 
    public string jobLocationCity { get; set; } 
    public string jobCountrySubdivision { get; set; } 
    public Board[] boards { get; set; } 
    public bool restrictionsIndicator { get; set; } 
    public int? personRestriction { get; set; } 
    public int? jobRestriction { get; set; } 
} 

public class Board 
{ 
    public int companyId { get; set; } 
    public string companyName { get; set; } 
    public int? companyOffLimits { get; set; } 
    public object restrictionCategoryId { get; set; } 
} 

该DLL是一个可移植的类库,它是.NET 4.5,我有JSON .net(10.0.1)通过nuget安装,但便携式库连接到Mac上的xamarin IOS项目。

如果被反序列化的JSON没有Boards它工作得很好,但如果有Board,我会收到以下消息。

Unable to find a constructor to use for type Board. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'results[1].boards[0].companyId' 

我使用的设置是:

var settings = new Newtonsoft.Json.JsonSerializerSettings 
{ 
    NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, 
    ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(), 

}; 

我曾尝试以下方法来得到它的序列:

  1. var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(_jsonString, settings);

  2. var jobject = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(_jsonString, new Rootobject());

我曾尝试以下

  • 放在一个默认的构造函数
  • 命名为班级的所有参数的构造函数
  • 添加属性到构造
  • 更换板卡列表
  • 取出板子属性

但仍然没有喜悦。它不会为我反序列化。

+0

“我收到以下消息” - 您没有发布错误消息的文本。当我认为你的意思是“反序列化”时,你还会使用“序列化”这个术语几次 - 这是令人困惑的。 – Jason

+0

对不起,试图一次做一百万件事。问题已更新。 – Qpirate

我认为你必须修改这个

public class Board 
{ 
    public int companyId { get; set; } 
    public string companyName { get; set; } 
    public int? companyOffLimits { get; set; } 
    public object restrictionCategoryId { get; set; } 

    **public Board(){}** 
} 

而且在其他类

或也改变

public Board[] boards { get; set; } 

public List<Board> boards { get; set; } 

尝试....

+0

Nope已经尝试过'放入默认构造函数' – Qpirate

+0

我已更新... –

+0

也尝试过'将主板更改为列表' – Qpirate