2015-04-17 61 views
0

所以我試圖讓這個JSON解析正確,但它只是崩潰。而http://json2csharp.com/不能給我正確的類多對象JSON到對象C#

這是JSON:

[ 
    { 
    "id": "2300", 
    "file_name": "2300_file", 
    "representations": { 
     "thumb": "thumb.jpeg", 
     "small": "small.jpeg", 
     "medium": "medium.jpeg", 
     "full": "2300.jpeg" 
    } 
    }, 
    { 
    "id": "2c00", 
    "file_name": "2c00_file", 
    "representations": { 
     "thumb": "thumb.jpeg", 
     "small": "small.jpeg", 
     "medium": "medium.jpeg", 
     "full": "2c00.jpeg" 
    } 
    }, 
    { 
    "id": "0800", 
    "file_name": "0800_file", 
    "representations": { 
     "thumb": "thumb.jpeg", 
     "small": "small.jpeg", 
     "medium": "medium.jpeg", 
     "full": "0800.jpeg" 
    } 
    } 
] 

,這裏是當前代碼我使用:

public class Representations 
{ 
    public string thumb { get; set; } 
    public string small { get; set; } 
    public string medium { get; set; } 
    public string full { get; set; } 
} 

public class Picture 
{ 
    public string id { get; set; } 
    public string file_name { get; set; } 
    public Representations representations { get; set; } 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    Picture ImageThing = JsonConvert.DeserializeObject<Picture>(InputBox.Text); //Imputbox is where the json resides 

    MessageBox.Show(ImageThing.file_name); 
} 

所以,我怎麼能做出的MessageBox分別輸出所有三個對象的file_name?

對不起,低質量的解釋,我累了,我只是想讓這個小東西工作。

+0

[在C#解析JSON]的可能重複(http://stackoverflow.com/questions/1212344/parse-json -in-c-sharp) – durron597

回答

2

這是指三個對象的JSON數組:

​​

所以你不能這樣反序列化到一個單獨的對象。它需要被反序列化到一個數組/列表由表示所述結果應是一個表:

List<Picture> pictures = JsonConvert.DeserializeObject<List<Picture>>(InputBox.Text); 
+0

它工作!非常感謝,這很簡單:D –