2015-10-28 61 views
0

反序列化JSON我有如何使用JSON.Net

[ 
    { 
    "Id": "a038ca5f-8ef6-41c1-a610-d76c26517058", 
    "MainCategoryName": "Digital", 
    "LastPublished": "20130731T15:09:51", 
    "SubCategories": [ 
     { 
     "Id": "39e4e9c2-0870-4906-99a4-938bf149a138", 
     "SubCategoryName": "Electrical and Electronics", 
     "LastPublished": "20130729T11:30:27" 
     }, 
     { 
     "Id": "41d23a7d-d70f-4e41-9cd3-ddede6f17370", 
     "SubCategoryName": "Information Technology", 
     "LastPublished": "20130814T16:55:53" 
     }, 
     { 
     "Id": "72d6904c-39be-4cf5-810a-e01e6df2b6a9", 
     "SubCategoryName": "Telecommunications", 
     "LastPublished": "20141209T15:02:07" 
     } 
    ] 
    }, 
    { 
    "Id": "536d443d-7a77-48b1-9721-0adabd4d11e7", 
    "MainCategoryName": "Dining", 
    "LastPublished": "20130729T10:38:25", 
    "SubCategories": [ 
     { 
     "Id": "e8a6b2af-188e-4143-a30f-15816ba10715", 
     "SubCategoryName": "Cafe", 
     "LastPublished": "20130828T17:02:23" 
     }, 
     { 
     "Id": "7fa05929-01f1-4aa5-bb0a-007bf5d75ca1", 
     "SubCategoryName": "Confectionery and Specialty", 
     "LastPublished": "20140715T17:16:08" 
     }, 
     { 
     "Id": "33470a6a-aa8b-4842-8ce7-57bb0f6c77ac", 
     "SubCategoryName": "Delivery", 
     "LastPublished": "20140715T17:17:40" 
     }, 
     { 
     "Id": "b5919130-5f78-4632-93e2-5bcedf663744", 
     "SubCategoryName": "Dessert Bar", 
     "LastPublished": "20150424T12:46:20" 
     }, 
     { 
     "Id": "e93d9457-f93e-4d23-b412-ffa9996ef8e1", 
     "SubCategoryName": "Fast Food", 
     "LastPublished": "20140715T17:18:58" 
     }, 
     { 
     "Id": "0860e413-f8fc-48aa-a24d-9f8e7eb98abd", 
     "SubCategoryName": "Food Kiosk and Takeaway", 
     "LastPublished": "20140715T17:19:23" 
     }, 
     { 
     "Id": "1188c663-2ea3-42a3-991c-9c9855801265", 
     "SubCategoryName": "Foodcourt", 
     "LastPublished": "20140715T17:19:31" 
     }, 
     { 
     "Id": "3bfbb105-3582-482e-a333-d987dc9bd70c", 
     "SubCategoryName": "Pub and Bar and Lounge", 
     "LastPublished": "20141027T12:02:42" 
     }, 
     { 
     "Id": "ed49e69d-38c6-4ebf-a844-03fab83854f7", 
     "SubCategoryName": "Restaurant", 
     "LastPublished": "20140715T17:20:02" 
     }, 
     { 
     "Id": "d7ae7119-7cd5-47c9-aa22-9094a91ac1bc", 
     "SubCategoryName": "Restaurant and Bar", 
     "LastPublished": "20140715T17:20:24" 
     }, 
     { 
     "Id": "ce0704aa-afcf-49ba-8d73-09ebe1f59624", 
     "SubCategoryName": "Restaurant with Live Band", 
     "LastPublished": "20140715T17:20:42" 
     }, 
     { 
     "Id": "3896e1bb-82aa-4866-a001-3d2c0d6f704b", 
     "SubCategoryName": "Quick Bite", 
     "LastPublished": "20141027T17:26:49" 
     }, 
     { 
     "Id": "e088dbc6-e4c0-4e39-9ba6-7cc46567408a", 
     "SubCategoryName": "Asian Delights", 
     "LastPublished": "20140715T17:20:50" 
     }, 
     { 
     "Id": "f0b6c043-60fc-46d7-b9dd-aa2f9f8a682b", 
     "SubCategoryName": "Chinese Cuisine", 
     "LastPublished": "20140715T17:16:14" 
     }, 
     { 
     "Id": "c4498d9d-b747-401d-a666-3c59819f44b7", 
     "SubCategoryName": "Western Delicacies", 
     "LastPublished": "20140715T17:16:16" 
     } 
    ] 
    } 
] 

而且我已經建立了類似的班MainCategory和下面子類別以下JSON數據:

public class MainCategory 
    { 
     string Id { get; set; } 
     string MainCategoryName { get; set; } 
     string LastPublished { get; set; } 
     IList<SubCategory> SubCategories { get; set; } 
    } 

public class SubCategory 
    { 
     string Id { get; set; } 
     string SubCategoryName { get; set; } 
     string LastPublished { get; set; } 
    } 

當我打電話List<MainCategory> mainCategories = JsonConvert.DeserializeObject<List<MainCategory>>(categoryJSON);,它確實創建了2個MainCategory對象,但是裏面的所有值都是'null'。我認爲我已經正確匹配了這個名稱,我可否知道是否有專家可以向我介紹一些情況?謝謝。

回答

1

得到了答案,只需要添加Public關鍵字到每個變量,更新代碼如下。

public class MainCategory 
    { 
     public string Id { get; set; } 
     public string MainCategoryName { get; set; } 
     public string LastPublished { get; set; } 
     public List<SubCategory> SubCategories { get; set; } 
    } 

public class SubCategory 
    { 
     public string Id { get; set; } 
     public string SubCategoryName { get; set; } 
     public string LastPublished { get; set; } 
    } 
1

您的課堂結構是正確的。我已經創建了類結構如下,它的工作原理。

public class SubCategory 
{ 
    public string Id { get; set; } 
    public string SubCategoryName { get; set; } 
    public string LastPublished { get; set; } 
} 

public class RootObject 
{ 
    public string Id { get; set; } 
    public string MainCategoryName { get; set; } 
    public string LastPublished { get; set; } 
    public List<SubCategory> SubCategories { get; set; } 
} 

現在反序列化使用

List<RootObject> mainCategories = JsonConvert.DeserializeObject<List<RootObject>>(categoryJSON); 

你缺少你的類屬性public訪問修飾符。

使用的生成類http://json2csharp.com/