I have been trying to mimic GoogleReader's Json output, particularly the subscription listing. I started out creating classes for deserialization but I don't have a full grasp of the concepts. It crashes when I really try to consume the Json file, but when I generate my own serialized file it looks just right. I don't know what's going wrong. Thus far this is the progress I made:

[DataContract(Name = "subscriptionsList")]
[Serializable]
public class Subscription
{
    [DataMember(Name = "subscriptions")]
    public List<SubscriptionItem> Subscriptions = new List<SubscriptionItem>();
    public Subscription()
    {
    }

}
[DataContract(Name="subscriptionitem")]
[Serializable]
public class SubscriptionItem
{
    [DataMember(Name = "id")]
    internal string id;
    public string Id
    {
        get
        {
            return id;
        }
        set
        {
            id = value;
        }
    }
        
    [DataMember(Name="title")]
    internal string title;
    public string Title
    {
        get
        {
            return title;
        }
        set
        {
            title = value;
        }
    }
        
    [DataMember(Name="sortid")]
    internal string sortid;
    public string SortId
    {
        get
        {
            return sortid;
        }
        set
        {
            sortid = value;
        }
    }
        
    [DataMember(Name="firstitemmsec")]
    internal string firstitemmsec;
    public string LatestUpdate
    {
        get
        {
            return firstitemmsec;
        }
        set
        {
            firstitemmsec = value;
        }
    }

    [DataMember(Name = "categories")]
    internal Categories categories;
    public Categories Categories
    {
        get
        {
            return categories;
        }
        set
        {
            categories = value;
        }
    }
}

[CollectionDataContract(Name = "categories")]
public class Categories : List<string>
{
    public Categories()
    : base()
    {
    }

    public Categories(string[] items)
    : base()
    {
        foreach (string item in items)
        {
            Add(item);
        }
    }
}

A comparison of the Json original format:

{"subscriptions":[{
	"id":"feed/http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/rss.xml",
	"title":"BBC Sport | Football | UK Edition",
	"categories":[],
	"sortid":"C6CEC826",
	"firstitemmsec":"1178119338469"
}]}

and the output of my DataContracts:

{"subscriptions":[{
	"categories":[],
	"firstitemmsec":null,
	"id":"0",
	"sortid":"0",
	"title":"0th"
}]}

I found one error: the DataMember attribute has to precede the class attributes or properties that are to be used for serialization/deserialization.
I fixed this and ran it through Windows Phone 7's own Json Deserializer method and it worked like clockwork. :)

Strangely though I still get the same error when I run this using the desktop .Net deserializer method for Json. I wish to learn why this is happening.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.