1

I have a POCO class which looks like this:

public class Item : Asset
{
    public int PlaylistId { get; set; }
    public int AssetId { get; set; }
    public double Duration { get; set; }
    public int Order { get; set; }
}

and Asset looks like this:

public enum AssetType
{
    Image = 1,
    Video,
    Website
}

public class Asset
{       

    public int Id { get; set; }
    public string Name { get; set; }
    public string Filename { get; set; }
    public AssetType Type { get; set; }
    public string CreatedById { get; set; }
    public string ModifiedById { get; set; }
    [Display(Name="Created by")] public string CreatedBy { get; set; }
    [Display(Name="Modified by")] public string ModifiedBy { get; set; }
}

and then I have a json file which looks like this:

{
   "Items":[
      {
         "PlaylistId":1,
         "Type":2,
         "Duration":19,
         "Filename":"stream1_mpeg4.avi"
      },
      {
         "PlaylistId":1,
         "Type":2,
         "Duration":21,
         "Filename":"stream2_mpeg4.avi"
      }
   ]
}

and finally I have my code which looks like this:

public IList<Item> GetAll()
{
    if (File.Exists(itemsPath))
    {
        using (var fs = new FileStream(itemsPath, FileMode.Open))
        using (var sr = new StreamReader(fs))
        {
            var text = sr.ReadToEnd();
            var array = JsonConvert.DeserializeObject<Item[]>(sr.ReadToEnd());
            return array.ToList();
        }
    }
    else
        throw new FileNotFoundException("Unable to find the playlist, please make sure that " + itemsPath + " exists.");
}

The text variable contains the correct json string as I would expect, but the array is null, therefore array.ToList(); throws an error. Does anyone know what I am doing wrong?

Cheers in advance /r3plica

2 Answers 2

3

You're calling ReadToEnd() twice, so the second time there's no more text to read on the stream:

var text = sr.ReadToEnd();
var array = JsonConvert.DeserializeObject<Item[]>(sr.ReadToEnd());

just replace the second sr.ReadToEnd() with text and it should work:

var array = JsonConvert.DeserializeObject<Item[]>(text);

Also, as correctly pointed out by @Sachin, your json represent an object with a property called Items that is an array or list of Item objects.
Therefore, you should pass through an intermediate class as shown in @Sachin's answer, or alternatively using a dictionary, like this:

var dict = JsonConvert.DeserializeObject<Dictionary<string,Item[]>>(text);
var array = dict["Items"];
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, works perfectly and I prefer your method over @Sachin simply because I don't like creating wrapper classes :o
2

You json string represents the serialization of an object which has a List<Item> as its property. That means you need to deserialize this string into such an object which has List<Item> as a property. So you can make a wrapper class like this

public class Wrapper
{
    public List<Item> items { get; set; }
}

and then Deserialize it like this

var array = JsonConvert.DeserializeObject<Wrapper>(text);

Now you can see your array has two elements inside it ie. array.Count=2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.