Newtonsoft.Json解析错误

时间:2014-09-25 21:10:28

标签: c# json.net

我正在尝试使用下面的代码解析一些JSON,但我收到以下错误...我添加了一个默认的空构造函数,但错误仍然存​​在。

content = Convert.ToString (content).Trim ();
result = JsonConvert.DeserializeObject<MyType> (content);

内容变量是:

  

{&#34; status&#34; :&#34;成功&#34;,&#34;结果&#34; :{&#34; identity_document&#34; :{&#34;数字&#34; :&#34; xx&#34;,&#34;输入&#34; :&#34; 02&#34;,&#34; country_of_issue&#34; :&#34; ZA&#34; },&#34; person&#34; :{&#34;姓&#34; :&#34; xx&#34;,&#34;缩写&#34; :&#34; xx&#34;,&#34; driver_restrictions&#34; :[&#34; 0&#34;,&#34; 0&#34;],&#34; date_of_birth&#34; :&#34; 05/11/1939&#34;,&#34; preferred_language&#34; :&#34;&#34;,&#34;性别&#34; :&#34;女性&#34; },&#34; driving_license&#34; :{&#34; certificate_number&#34; :&#34; xx&#34;,&#34; country_of_issue&#34; :&#34; ZA&#34; },&#34; card&#34; :{&#34; issue_number&#34; :&#34; 02&#34;,&#34; date_valid_from&#34; :&#34; 19/05 / 2001&#34;,&#34; date_valid_until&#34; :&#34; 19/05/2006&#34; },&#34; professional_driving_permit&#34; :&#34; nil&#34;,&#34; vehicle_classes&#34; :[{&#34;代码&#34; :&#34; EB&#34;,&#34; vehicle_restriction&#34; :&#34; 0&#34;,&#34; first_issue_date&#34; :&#34; 18/05 / 2001&#34; }],&#34;照片&#34; :&#34; xxx&#34; }}

错误:

  

无法找到用于MyType类型的构造函数。一堂课应该   或者有一个默认的构造函数,一个带参数的构造函数或者   用JsonConstructor属性标记的构造函数。路径状态&#39;,   第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;
  }
}

1 个答案:

答案 0 :(得分:0)

似乎可以正常使用以下SSCCE与最新版本的Json.Net(6.0.5)和.NET 4.5。尝试在您的环境中运行此代码以查看它是否有效。这应该。如果是这样,请尝试逐位修改它,直到它更接近您的代码。一旦它停止工作,你就发现了问题。如果代码在您的环境中不起作用而没有任何修改,那么它与您的环境有关。也许是Json.Net的旧版本?

在这里小提琴: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