我试图用下面的代码解析一些JSON,但我得到了下面的错误。。。我添加了一个默认的空构造函数,但错误仍然存在。
content = Convert.ToString (content).Trim ();
result = JsonConvert.DeserializeObject<MyType> (content);
内容变量为:
{"status":"success","result":{"identity_document":"number":"xx","type":"02","country_of_pissue":"ZA"},"person":"姓氏":"xx","缩写":"xxx","driver_restrictions":["0","0"],"date_of_production":"05/11939","preferred_language":":"ZA"},"卡片":{"issue_number":"02","date_valid_from":"19/05/2001","date_valid_until":"19/05/2006"},"professional_driveing_permit":"nil","vehicle_classes":[{"code":"EB","vehicle_restriction":"0","first_issue_date":"18/05/2001"}],"photo":"xxx"}}
错误:
找不到用于类型MyType的构造函数。一个班级应该有一个默认构造函数、一个带参数的构造函数或用JsonConstructor属性标记的构造函数。路径"status",第1行,位置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;
}
}
在最新版本的Json.Net(6.0.5)和.Net 4.5中使用以下SSCCE似乎效果良好。请尝试在您的环境中运行此代码,看看它是否有效。它应该。如果是,请尝试一点一点地修改它,直到它更像您的代码。一旦它停止工作,你就发现了问题。如果代码在没有修改的情况下不能在您的环境中工作,那么它就是您的环境的问题。也许是Json.Net的旧版本?
Fiddle here:https://dotnetfiddle.net/cAk11I
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
string json = @"
{
""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""
}
}";
var root = JsonConvert.DeserializeObject<MyType>(json);
Console.WriteLine("ID doc number: " + root.result.identity_document.number);
Console.WriteLine("ID doc type: " + root.result.identity_document.type);
Console.WriteLine("ID doc country: " + root.result.identity_document.country_of_issue);
Console.WriteLine("person surname: " + root.result.person.surname);
Console.WriteLine("person DOB: " + root.result.person.date_of_birth);
Console.WriteLine("person gender: " + root.result.person.gender);
Console.WriteLine("D.L. cert number: " + root.result.driving_license.certificate_number);
Console.WriteLine("card valid from: " + root.result.card.date_valid_from);
Console.WriteLine("card valid to: " + root.result.card.date_valid_until);
Console.WriteLine("vehicle class code: " + root.result.vehicle_classes.First().code);
Console.WriteLine("photo: " + root.result.photo);
}
}
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;
}
}
public class Result
{
public IdentityDocument identity_document { get; set; }
public Person person { get; set; }
public DrivingLicense driving_license { get; set; }
public Card card { get; set; }
public string professional_driving_permit { get; set; }
public List<VehicleClass> vehicle_classes { get; set; }
public string photo { get; set; }
}
public class IdentityDocument
{
public string number { get; set; }
public string type { get; set; }
public string country_of_issue { get; set; }
}
public class Person
{
public string surname { get; set; }
public string initials { get; set; }
public List<string> driver_restrictions { get; set; }
public string date_of_birth { get; set; }
public string preferred_language { get; set; }
public string gender { get; set; }
}
public class DrivingLicense
{
public string certificate_number { get; set; }
public string country_of_issue { get; set; }
}
public class Card
{
public string issue_number { get; set; }
public string date_valid_from { get; set; }
public string date_valid_until { get; set; }
}
public class VehicleClass
{
public string code { get; set; }
public string vehicle_restriction { get; set; }
public string first_issue_date { get; set; }
}
输出:
ID doc number: xx
ID doc type: 02
ID doc country: ZA
person surname: xx
person DOB: 05/11/1939
person gender: FEMALE
D.L. cert number: xx
card valid from: 19/05/2001
card valid to: 19/05/2006
vehicle class code: EB
photo: xxx