0

I'm having issues with deserializing a nested array of JSON objects in c# using JavascriptDeserializer

Here is my code

  using (Stream s = request.GetResponse().GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(s))
                    {
                        string jsondata = sr.ReadToEnd();

                        var workout = ser.Deserialize<clServiceOutput1>(jsondata);
                    }
                }

Here is my Jsondata

{"Data":"50951","FileData":[37,80,68,70,45,49,46,51,13,37,226,227,207,211,13,10],"MailItem":null,"Status":"Success","TurnAroundTime":null}

Here is my class

public class clServiceOutput1
    {
        public string Data { get; set; }
        public string FileData { get; set; }
        public string MailItem { get; set; }
        public string Status { get; set; }
        public string TurnAroundTime { get; set; }

    }
Kangaroo
  • 79
  • 1
  • 12

1 Answers1

2

FileData is a collection of numeric values in your json string.

"FileData":[37,80,68,70,45,49,46,51,13,37,226,227,207,211,13,10]

You need

List<int> FileData //or int[]

As a side note, use http://json2csharp.com/ to copy your json and get a C# template class back. Pasting your JSON in the above site results in:

public class RootObject
{
    public string Data { get; set; }
    public List<int> FileData { get; set; }
    public object MailItem { get; set; }
    public string Status { get; set; }
    public object TurnAroundTime { get; set; }
}

Based on comment from @xanatos

By the name of the field, it seems to be the binary "stream" of a file, not something that must be expanded. So a byte[] could also be the type of your field

Community
  • 1
  • 1
Habib
  • 205,061
  • 27
  • 376
  • 407
  • or `"[37,80,68,70,45,49,46,51,13,37,226,227,207,211,13,10]"` :-D – Grundy May 15 '15 at 13:22
  • 1
    or even an `int[]` probably – xanatos May 15 '15 at 13:22
  • @xanatos, definitely yes. But I personally prefer `List` over array most of the time. *(based on [this](http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which))* – Habib May 15 '15 at 13:25
  • 1
    @Habib By the name of the field, it seems to be the binary "stream" of a file, not something that must be expanded. So a `byte[]` – xanatos May 15 '15 at 13:26
  • @xanatos, that is even more probable. I should add that to the answer – Habib May 15 '15 at 13:29