3

I'm getting the JSON output as follows:

{"data":
 [
   {
    "name":"followersQuery",
    "fql_result_set":[{"subscriber_count":300}]
    },
   {
   "name":"likesQuery",
   "fql_result_set":[{"like_count":0}]
   }
 ]
}

It is the output of multiple fql query.

I have created the following classes to deserialize the output:

public class ResultCount
    {
        [JsonProperty("subscriber_count")]
        public int Followers { get; set; }

        [JsonProperty("like_count")]
        public int Likes { get; set; }
    }

    public class ResultItem
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("fql_result_set")]
        public ResultCount ResultCounts { get; set; }
    }

    public class FacebookData
    {
        public List<ResultItem> Items { get; set; }
    }

I'm getting error while de-serializing the output in the following line:

 JsonConvert.DeserializeObject<FacebookData>(myOutput);

The error is:

The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject(string)' has some invalid arguments.

Not able to correct this. Can anyone please correct this?

Many many thanks in advance!

Dukhabandhu Sahoo
  • 1,394
  • 1
  • 22
  • 44

1 Answers1

3

ResultCounts return type should be List<Resultcount>.

Change

public ResultCount ResultCounts { get; set; }

to

public List<ResultCount> ResultCounts { get; set; }

On a sidenote you can get class structue by simply pasting your json here - Json2csharp. It will automatically generate class structure for you. It can be use to validate your structure. Generated structure was like this:

public class FqlResultSet
{
    public int subscriber_count { get; set; }
    public int? like_count { get; set; }
}

public class Datum
{
    public string name { get; set; }
    public List<FqlResultSet> fql_result_set { get; set; }
}

public class RootObject
{
    public List<Datum> data { get; set; }
}
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185