2017-02-21 126 views
-2

你好我有填充我的JSON文件到C#對象的嚴重問題,因爲JSON包含「數組數組」。我需要用所有數組創建類,以便能夠在反序列化之後將文件加載到那裏,但是我不知道如何創建這樣的結構。反序列化JSON到C#對象與數組數組

JSON文件:

{ 
    "status": true, 
    "result": [ 
     { 
      "ID": "1", 
      "UserID": "1001", 
      "web_id": "2753", 
      "certificate_id": "31", 
      "domain": "testsomething.com", 
      "status": "ssl_uninstalling", 
      "updated_at": "2017-02-15 15:08:04", 
      "IP": "xx.x.xxx.xxx", 
      "OS_version": "CentOS release 4.8" 
     }, 
     { 
      "ID": "2", 
      "UserID": "1001", 
      "web_id": "2753", 
      "certificate_id": "31", 
      "domain": "admin.testsomething.com", 
      "status": "ssl_uninstalling", 
      "updated_at": "2017-02-13 14:19:45", 
      "IP": "xx.x.xxx.xxx", 
      "OS_version": "CentOS release 4.8" 
     }, 
     { 
      "ID": "3", 
      "UserID": "1001", 
      "web_id": "2753", 
      "certificate_id": "31", 
      "domain": "www.testsomething16.com", 
      "status": "ssl_uninstalling", 
      "updated_at": "2017-02-16 16:22:40", 
      "IP": "xx.x.xxx.xxx", 
      "OS_version": "CentOS release 4.8" 
     }, 
     { 
      "ID": "4", 
      "UserID": "1001", 
      "web_id": "2753", 
      "certificate_id": "31", 
      "domain": "blog.testsomething.com", 
      "status": "ssl_uninstalling", 
      "updated_at": "2017-02-16 16:22:40", 
      "IP": "xx.x.xxx.xxx", 
      "OS_version": "CentOS release 4.8" 
     } 
    ], 
    "errors": [] 
} 
+2

http://json2csharp.com/ –

+1

在Visual Studio中,它只是複製'SpecialPaste Json作爲類 –

+0

您是否想要一個工具來完成所有工作(這很好,直到JSON發生變化,但是如果JSON一致,那麼會好的),還是你想要控制細節的代碼? – Richard

回答

1

您的JSON結構看起來像

public class Result 
{ 

    [JsonProperty("ID")] 
    public string ID { get; set; } 

    [JsonProperty("UserID")] 
    public string UserID { get; set; } 

    [JsonProperty("web_id")] 
    public string web_id { get; set; } 

    [JsonProperty("certificate_id")] 
    public string certificate_id { get; set; } 

    [JsonProperty("domain")] 
    public string domain { get; set; } 

    [JsonProperty("status")] 
    public string status { get; set; } 

    [JsonProperty("updated_at")] 
    public string updated_at { get; set; } 

    [JsonProperty("IP")] 
    public string IP { get; set; } 

    [JsonProperty("OS_version")] 
    public string OS_version { get; set; } 
} 

public class Rootobject 
{ 

    [JsonProperty("status")] 
    public bool status { get; set; } 

    [JsonProperty("result")] 
    public List<Result> result { get; set; } 

    [JsonProperty("errors")] 
    public List<object> errors { get; set; } 
} 

您可以使用Visual Studio爲您創建JSON之外的類。 將您的JSON複製到剪貼板中。這意味着剛剛CTRL + Ç,然後去到Visual Studio編輯 - >選擇性粘貼 - >粘貼JSON作爲類

另一條出路,以創造一流的內在張力結構爲您的JSON可能是你可以去json2csharp: generate c# classes from json創造類。

+0

非常感謝。它很容易,我花了很多時間。 –

0

我想你可以創建一個像下面的模型:

public class Example 
    { 
     public bool Status { get; set; } 
     public ResultModel ResultModel { get; set; } 
     public bool Error { get; set; } 

     public Example() 
     { 
      ResultModel = new ResultModel(); 
     } 
    } 

    public class ResultModel 
    { 
     public int ID { get; set; } 
     public int UserId { get; set; } 
     public int WebId { get; set; } 
    }