0

So i'm trying to get this JSON to parse correctly, but it just crashes. And http://json2csharp.com/ is not able to give me the right classes

This is the 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"
    }
  }
]

And here is the current code I use:

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);
}

So how can I make the MessageBox output the file_name of all the three objects individually?

Sorry for the low quality explanation, I'm tired and I just want to get this little thing working.

Isa
  • 248
  • 2
  • 9
  • possible duplicate of [Parse JSON in C#](http://stackoverflow.com/questions/1212344/parse-json-in-c-sharp) – durron597 Apr 20 '15 at 20:16

1 Answers1

2

This denotes a JSON array of three objects:

[
  {
     ...
  },
  {
     ...
  },
  {
     ...
  }
]

So you cannot deserialize this to a single object. It needs to be deserialized to an array/list by indicating the result should be a List:

List<Picture> pictures = JsonConvert.DeserializeObject<List<Picture>>(InputBox.Text);
AaronLS
  • 37,329
  • 20
  • 143
  • 202