2015-04-17 62 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 –