Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.



My Code works fine when calling REST URL:
http://api.feedzilla.com/v1/categories.json

but when I call following URL I get error:
http://api.feedzilla.com/v1/categories/15/articles.json?count=36&since=2012-11-15&client_source=&order=relevance&title_only=0&

Error:

{"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.IEnumerable`1[Nitin.News.DAL.Resources.Article]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'articles', line 1, position 12."}

My Code is as follows:

    public class Article
    {
        public string publish_date { get; set; }
        public string source { get; set; }
        public string source_url { get; set; }
        public string summary { get; set; }
        public string title { get; set; }
        public string url { get; set; }
    }
    public IEnumerable<Article> Get()
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://api.feedzilla.com/v1/");

        //Add an Accept header for JSON format.
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        // call the REST method
        HttpResponseMessage response = client.GetAsync("http://api.feedzilla.com/v1/categories/2/articles.json??count=36&since=2012-11-15&client_source=&order=relevance&title_only=0&").Result;  // Blocking call!
        if (response.IsSuccessStatusCode)
        {
            // Parse the response body. Blocking!
            return response.Content.ReadAsAsync<IEnumerable<Article>>().Result;

            //wont work
            //string JSON =response.Content.ReadAsStringAsync().Result;
            //return JsonConvert.DeserializeObject<IEnumerable<T>>(JSON);
        }
        else
        {
            throw new Exception(string.Format("Data access faild,{0} ({1}) method:{2}", (int)response.StatusCode, response.ReasonPhrase, MethodURL));
        }
    }
share|improve this question

1 Answer

up vote 1 down vote accepted

You need another level in your object hierachy... i.e. a root to contain the IEnumerable.

Runing the JSON into the tool at http://json2csharp.com/ generates the following proxy classes:

public class Enclosure
{
    public int length { get; set; }
    public string media_type { get; set; }
    public string uri { get; set; }
}

public class Article
{
    public string author { get; set; }
    public string publish_date { get; set; }
    public string source { get; set; }
    public string source_url { get; set; }
    public string summary { get; set; }
    public string title { get; set; }
    public string url { get; set; }
    public List<Enclosure> enclosures { get; set; }
}

public class RootObject
{
    public List<Article> articles { get; set; }
    public string description { get; set; }
    public string syndication_url { get; set; }
    public string title { get; set; }
}

You just need to change your code to this then:

// Parse the response body. Blocking!
return response.Content.ReadAsAsync<RootObject>().Result.articles;

Obviously strip out any properties you dont need. Just thought I would show them all out of interest.

share|improve this answer
Thanks for your reply, but unfortunately I am getting the same error message. – Nitin Nov 17 '12 at 13:53
@NitinJS sorry to hear that. I had tested the code and went from having your error to resolving it. I can upload the full working project if you like? Did you change this line of code? esponse.Content.ReadAsAsync<RootObject>()? – Mark Jones Nov 17 '12 at 13:55
Hi, Thanks, your code worked :D changed the return type from IEnumerable<T> to T – Nitin Nov 17 '12 at 14:05
Ok great, glad it helped. – Mark Jones Nov 17 '12 at 14:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.