我想解析下面的代碼的一些JSON,但我得到下面的錯誤...我添加了一個默認的空構造函數,但錯誤仍然存在。Newtonsoft.Json解析錯誤
content = Convert.ToString (content).Trim();
result = JsonConvert.DeserializeObject<MyType> (content);
內容變量是:
{ "status" : "success", "result" : { "identity_document" : { "number" : "xx", "type" : "02", "country_of_issue" : "ZA" }, "person" : { "surname" : "xx", "initials" : "xx", "driver_restrictions" : ["0", "0"], "date_of_birth" : "05/11/1939", "preferred_language" : "", "gender" : "FEMALE" }, "driving_license" : { "certificate_number" : "xx", "country_of_issue" : "ZA" }, "card" : { "issue_number" : "02", "date_valid_from" : "19/05/2001", "date_valid_until" : "19/05/2006" }, "professional_driving_permit" : "nil", "vehicle_classes" : [{ "code" : "EB", "vehicle_restriction" : "0", "first_issue_date" : "18/05/2001" }], "photo" : "xxx" } }
錯誤:
Unable to find a constructor to use for type MyType. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'status', line 1, position 12.
public class MyType
{
public string status { get; set; }
public Result result { get; set; }
[JsonConstructor]
public MyType()
{ }
public MyType(string aStatus, Result aResult) {
this.status = aStatus;
this.result = aResult;
}
}
你是什麼參數的構造函數的可及性?如果你展示一個簡短但完整的程序來展示問題,那真的很有幫助。 – 2014-09-25 21:12:35
您是否曾嘗試向錯誤消息提示的無參數構造函數中添加'[JsonConstructor]'屬性? – 2014-09-25 21:18:28
同意Jon。爲了解決這個問題,我們需要看到「內容」和MyType類的價值。 – Kevin 2014-09-25 21:19:07