所以我试图让这个JSON正确解析,但它只是崩溃了。 http://json2csharp.com/无法为我提供正确的课程
这是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 :(得分:2)
这表示包含三个对象的JSON数组:
[
{
...
},
{
...
},
{
...
}
]
因此,您无法将其反序列化为单个对象。需要通过指示结果应该是List:
将其反序列化为数组/列表List<Picture> pictures = JsonConvert.DeserializeObject<List<Picture>>(InputBox.Text);