3

I'm working with NewsBlur API which returns a feed list. The response JSON is of type:

{
    "authenticated": true,
    "user": "sathyabhat",
    "feeds": {},
    "flat_folders": { },
    result: "ok"
}

where feeds: is of type

    "feeds": {
        "96705": {
            "feed_address": "http://www.linuxhaxor.net/",
            "updated": "8492 hours",
            "favicon_text_color": null,
            "subs": 35,
            "feed_link": "http://www.linuxhaxor.net/",
            "favicon_fetching": true,
            "nt": 0,
            "updated_seconds_ago": 30573336,
            "num_subscribers": 35,
            "feed_title": "LinuxHaxor.net",
            "favicon_fade": null,
            "exception_type": "feed",
            "exception_code": 503,
            "favicon_color": null,
            "active": true,
            "ng": 0,
            "feed_opens": 0,
            "id": 96705,
            "ps": 0,
            "has_exception": true
        },
        "768840": {
            "feed_address": "http://feeds.feedburner.com/PathikShahDotCom",
            "updated": "3 hours",
            "favicon_text_color": "black",
            "subs": 1,
            "feed_link": "http://www.pathikshah.com/blog",
            "favicon_fetching": false,
            "nt": 0,
            "updated_seconds_ago": 13043,
            "num_subscribers": 1,
            "feed_title": "Pathik Shah",
            "favicon_fade": "769456",
            "favicon_color": "b2d092",
            "active": true,
            "ng": 0,
            "feed_opens": 0,
            "id": 768840,
            "ps": 0
        },
        "768842": {
            "feed_address": "http://feeds.preshit.net/preshit/blog",
            "updated": "3 hours",
            "favicon_text_color": null,
            "subs": 1,
            "feed_link": "http://preshit.net",
            "favicon_fetching": false,
            "nt": 3,
            "updated_seconds_ago": 13536,
            "num_subscribers": 1,
            "feed_title": "Preshit Deorukhkar",
            "favicon_fade": null,
            "favicon_color": null,
            "active": true,
            "ng": 0,
            "feed_opens": 1,
            "id": 768842,
            "ps": 0
        },
        "768843": {
            "feed_address": "http://quizwith.net/feed/",
            "updated": "3 hours",
            "favicon_text_color": "white",
            "subs": 1,
            "feed_link": "http://quizwith.net",
            "favicon_fetching": false,
            "nt": 0,
            "updated_seconds_ago": 11617,
            "num_subscribers": 1,
            "feed_title": "quizwith.net",
            "favicon_fade": "c22900",
            "favicon_color": "fe6501",
            "active": true,
            "ng": 0,
            "feed_opens": 0,
            "id": 768843,
            "ps": 0
        }

[... so on ..]

    }

So essentially, it's a list of feeds objects. Now, I'm trying to deserialize this into a C# object using JSON.net, but my class members are populated with null values.

My class is modelled on the response that I'm getting:

 public class FeedResponse
    {
   public string authenticated { get; set; }
   public string user { get; set; }
   public List<feed> feeds { get; set; }
   public string flat_folders { get; set; }
   public string result { get; set; }
}

public class feed
{
    public string feed_address { get; set; }
    public string updated { get; set; }
    public string favicon_text_color { get; set; }
    public int subs { get; set; }
    public string feed_link { get; set; }
    public bool favicon_fetching { get; set; }
    public string nt { get; set; }
    public string updated_seconds_ago { get; set; }
    public int num_subscribers { get; set; }
    public string feed_title { get; set; }
    public string favicon_fade { get; set; }
    public string exception_type { get; set; }
    public string exception_code { get; set; }
    public string favicon_color { get; set; }
    public bool active { get; set; }
    public string ng { get; set; }
    public int feed_opens { get; set; }
    public int id { get; set; }
    public bool has_exception { get; set; }

}

This is the line which attempts to deserialize( using RestSharp):

 return Execute<FeedResponse>(request);

Evidently, something goes wrong while trying to parse the JSON for the feed response.

Any pointers as to what I'm doing wrong?

4
  • have you tried to deserialize it into a dictionary? Commented Dec 7, 2011 at 18:08
  • @DanielA.White - I'm not sure how a dictionary will fit the data? Commented Dec 7, 2011 at 18:21
  • have you tried building a feedcollection via code and serialising to see what the difference is? Commented Dec 9, 2011 at 14:37
  • @Sathya if you haven't gotten this by your saturday morning, lemme know on skype Commented Dec 9, 2011 at 21:31

3 Answers 3

5
+100

Feeds is a Dictionary<int,Feed> not a List<Feed> (theres is Keys).

edit: a little more details, a list of objects in json would look like that:

[{ name: 'elem1'},
{ name: 'elem2'},
{ name: 'elem3'}]

edit2: full sample working:

 using System; 
 using System.Collections.Generic; 
 using System.Linq;
 using System.Web.Script.Serialization;

 namespace JSON {
     class Program
     {
         static void Main(string[] args)
         {
             var json = @"{
     ""authenticated"": true,
     ""user"": ""sathyabhat"",
     ""feeds"": {
         ""96705"": {
             ""feed_address"": ""http://www.linuxhaxor.net/"",
             ""updated"": ""8492 hours"",
             ""favicon_text_color"": null,
             ""subs"": 35,
             ""feed_link"": ""http://www.linuxhaxor.net/"",
             ""favicon_fetching"": true,
             ""nt"": 0,
             ""updated_seconds_ago"": 30573336,
             ""num_subscribers"": 35,
             ""feed_title"": ""LinuxHaxor.net"",
             ""favicon_fade"": null,
             ""exception_type"": ""feed"",
             ""exception_code"": 503,
             ""favicon_color"": null,
             ""active"": true,
             ""ng"": 0,
             ""feed_opens"": 0,
             ""id"": 96705,
             ""ps"": 0,
             ""has_exception"": true
         },
         ""768840"": {
             ""feed_address"": ""http://feeds.feedburner.com/PathikShahDotCom"",
             ""updated"": ""3 hours"",
             ""favicon_text_color"": ""black"",
             ""subs"": 1,
             ""feed_link"": ""http://www.pathikshah.com/blog"",
             ""favicon_fetching"": false,
             ""nt"": 0,
             ""updated_seconds_ago"": 13043,
             ""num_subscribers"": 1,
             ""feed_title"": ""Pathik Shah"",
             ""favicon_fade"": ""769456"",
             ""favicon_color"": ""b2d092"",
             ""active"": true,
             ""ng"": 0,
             ""feed_opens"": 0,
             ""id"": 768840,
             ""ps"": 0
         },
         ""768842"": {
             ""feed_address"": ""http://feeds.preshit.net/preshit/blog"",
             ""updated"": ""3 hours"",
             ""favicon_text_color"": null,
             ""subs"": 1,
             ""feed_link"": ""http://preshit.net"",
             ""favicon_fetching"": false,
             ""nt"": 3,
             ""updated_seconds_ago"": 13536,
             ""num_subscribers"": 1,
             ""feed_title"": ""Preshit Deorukhkar"",
             ""favicon_fade"": null,
             ""favicon_color"": null,
             ""active"": true,
             ""ng"": 0,
             ""feed_opens"": 1,
             ""id"": 768842,
             ""ps"": 0
         },
         ""768843"": {
             ""feed_address"": ""http://quizwith.net/feed/"",
             ""updated"": ""3 hours"",
             ""favicon_text_color"": ""white"",
             ""subs"": 1,
             ""feed_link"": ""http://quizwith.net"",
             ""favicon_fetching"": false,
             ""nt"": 0,
             ""updated_seconds_ago"": 11617,
             ""num_subscribers"": 1,
             ""feed_title"": ""quizwith.net"",
             ""favicon_fade"": ""c22900"",
             ""favicon_color"": ""fe6501"",
             ""active"": true,
             ""ng"": 0,
             ""feed_opens"": 0,
             ""id"": 768843,
             ""ps"": 0
         }
     },
     ""flat_folders"": { },
     result: ""ok"" }";

             var jsonSerializer = new JavaScriptSerializer();
             var response = jsonSerializer.Deserialize<FeedResponse(json);
             Console.Write(jsonSerializer.Serialize(response));
             Console.ReadKey();
         }
     }

     public class FeedResponse
     {
         public bool authenticated { get; set; }
         public string user { get; set; }
         public Dictionary<string,feed> feeds { get; set; }
         public object flat_folders { get; set; }
         public string result { get; set; }
     }

     public class feed
     {
         public string feed_address { get; set; }
         public string updated { get; set; }
         public string favicon_text_color { get; set; }
         public int subs { get; set; }
         public string feed_link { get; set; }
         public bool favicon_fetching { get; set; }
         public string nt { get; set; }
         public string updated_seconds_ago { get; set; }
         public int num_subscribers { get; set; }
         public string feed_title { get; set; }
         public string favicon_fade { get; set; }
         public string exception_type { get; set; }
         public string exception_code { get; set; }
         public string favicon_color { get; set; }
         public bool active { get; set; }
         public string ng { get; set; }
         public int feed_opens { get; set; }
         public int id { get; set; }
         public bool has_exception { get; set; }
     } }
Sign up to request clarification or add additional context in comments.

3 Comments

Do you need additional information to make the answer complete?
No, this pretty much answers my question very well, thank you!
woops, slipped my mind. Thanks for the reminder! @Guillaume86
1

Use a jsonreader, using json.net, your example should look like this:

public class Feeds
{
    public bool authenticated;
    public string user;
    public Dictionary<string, Feed> feeds;


    public void ReadJson(JsonReader reader)
    {
        while (reader.Read())
        {
            if (reader.TokenType == JsonToken.EndObject)
                break;

            if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "authenticated")
            {
                reader.Read();
                authenticated = (bool)reader.Value;
            }
            else if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "user")
            {
                reader.Read();
                user = (string)reader.Value;
            }
            else if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "feeds")
            {
                feeds = new Dictionary<string, Feed>();
                while (reader.Read())
                {
                    if (reader.TokenType == JsonToken.EndObject)
                        break;

                    if (reader.TokenType == JsonToken.PropertyName)
                    {
                        string key = (string)reader.Value;
                        feeds.Add(key, Feed.ReadFromJson(reader));
                    }
                }

            }
        }
    }

    public static Feeds ReadFromJson(Stream stream)
    {
        using (StreamReader sr = new StreamReader(stream))
        {
            using (Newtonsoft.Json.JsonReader jsonReader = new Newtonsoft.Json.JsonTextReader(sr))
            {
                Feeds feeds = new Feeds();
                feeds.ReadJson(jsonReader);
                return feeds;
            }
        }
    }
}

public class Feed
{
    public string feed_address;
    public string updated;

    public void ReadJson(JsonReader reader)
    {
        while (reader.Read())
        {
            if (reader.TokenType == JsonToken.EndObject)
                break;

            if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "feed_address")
            {
                reader.Read();
                feed_address = (string)reader.Value;
            }
            else if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "updated")
            {
                reader.Read();
                updated = (string)reader.Value;
            }
        }
    }

    public static Feed ReadFromJson(JsonReader reader)
    {
        Feed feed = new Feed();
        feed.ReadJson(reader);
        return feed;
    }
}

Comments

1

Since your returned Json string contains names like 96705,768840,768842,768843 which are not valid c# variable names, you can not deserialize your string to a class unless you use a dictionary for feeds. So, if accessing some of the properties of your object as dict["key"] is inevitable, then why bother with declaring classes FeedResponse or feed anyway? Just use Json.Net's JObject, JToken, JProperty as below.

JObject rootObj =  JsonConvert.DeserializeObject(jstr) as JObject;
Console.WriteLine(
    "Authenticated:" + rootObj["authenticated"] + 
    " USER:"         + rootObj["user"] + 
    " RESULT:"       + rootObj["result"]);


foreach (JProperty feed in rootObj["feeds"])
{
    Console.WriteLine(feed.Name + " " +  feed.Value["feed_address"]);
}

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.