【问题标题】:Error while deserializing the JSON反序列化 JSON 时出错
【发布时间】:2013-12-25 13:29:55
【问题描述】:

我得到如下 JSON 输出:

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

是多个fql查询的输出。

我创建了以下类来反序列化输出:

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; }
    }

我在反序列化以下行中的输出时遇到错误:

 JsonConvert.DeserializeObject<FacebookData>(myOutput);

错误是:

最好的重载方法匹配 'Newtonsoft.Json.JsonConvert.DeserializeObject(字符串)' 有一些无效的参数。

无法更正此问题。有人可以纠正这个吗?

提前非常感谢!

【问题讨论】:

    标签: c# json deserialization


    【解决方案1】:

    ResultCounts 返回类型应为List&lt;Resultcount&gt;

    改变

    public ResultCount ResultCounts { get; set; }
    

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

    在旁注中,您只需在此处粘贴您的 json -Json2csharp 即可获得类结构。它会自动为你生成类结构。它可用于验证您的结构。生成的结构是这样的:

    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 非常感谢!
    猜你喜欢
    • 2017-05-10
    • 2019-12-08
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多