2012-12-06 62 views
1

我有一個C#類:轉換unwellformatted JSON到C#類

public class JsonAttachmentModel 
{ 
    public Guid AttachmentId { get; set; } 
    public string FileName { get; set; } 
    public string ExtentionName { get; set; } 
} 

,我檢索JSON是unwellformatted,它是完全在一個字符串,如:

{"success":false,"message":"The remote server returned an unexpected response: (413) Request Entity Too Large."}, 
{"success":true,"attachmentId":"dba60830-46fd-4f51-9cd5-3c20df03988d","fileName":"bz1_20121206113251.PNG","extentionName":".PNG"}, 
{"success":true,"attachmentId":"3cae3f50-7d70-4183-9a69-80cb6d619dbd","fileName":"bz2_20121206113251.PNG","extentionName":".PNG"}, 
{"success":true,"attachmentId":"b6c8a524-4f46-4a7a-bcda-70042cb4ca92","fileName":"IammorethanOneKb123_20121206113251.txt","extentionName":".txt"}, 
{"success":true,"attachmentId":"994dfb9c-a185-4c51-b1f4-1017f991740d","fileName":"Iamonekb_20121206113251.txt","extentionName":".txt"} 

所以,我的問題是:

我怎樣才能把這個字符串轉換List<JsonAttachmentModel>,與只匹配「成功」的項目:真正

+1

使用JSON.NET .... –

回答

2

使用JSON.NET。查看here的序列化/反序列化示例。從上面的文檔

示例 -

string json = @"[ 
    { 
     'Name': 'Product 1', 
     'ExpiryDate': '2000-12-29T00:00Z', 
     'Price': 99.95, 
     'Sizes': null 
    }, 
    { 
     'Name': 'Product 2', 
     'ExpiryDate': '2009-07-31T00:00Z', 
     'Price': 12.50, 
     'Sizes': null 
    } 
]"; 

List<Product> products = JsonConvert.DeserializeObject<List<Product>>(json); 

Console.WriteLine(products.Count); 
// 2 

Product p1 = products[0]; 

Console.WriteLine(p1.Name); 
// Product 1 
+1

是的,我想這一點。它工作得很好。非常感謝你!你至少在這工作上節省了3個工作小時:) –