2014-09-25 84 views
-3

我想解析下面的代碼的一些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; 
    } 
} 
+3

你是什麼參數的構造函數的可及性?如果你展示一個簡短但完整的程序來展示問題,那真的很有幫助。 – 2014-09-25 21:12:35

+1

您是否曾嘗試向錯誤消息提示的無參數構造函數中添加'[JsonConstructor]'屬性? – 2014-09-25 21:18:28

+0

同意Jon。爲了解決這個問題,我們需要看到「內容」和MyType類的價值。 – Kevin 2014-09-25 21:19:07

回答

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