【问题标题】:JsonConvert.DeserializeObject<T> return nullJsonConvert.DeserializeObject<T> 返回 null
【发布时间】:2016-03-24 06:04:33
【问题描述】:

JsonConvert.DeserializeObject 返回 null,我不知道为什么?

   public class FriendsData
   {
    public Friend friend { get; set; }
   }

   [JsonObject]
   public class Friend
   {
        [JsonProperty("count")]
        public string count { get; set; }

        [JsonProperty("items")]
        public List<Items> items { get; set; }
   }

   public class Items
   {
    [JsonProperty("first_name")]
    public string first_name { get; set; }

    [JsonProperty("last_name")]
    public string last_name { get; set; }

    [JsonProperty("id")]
    public string id { get; set; }

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

    [JsonProperty("hidden")]
    public string hidden { get; set; }

    [JsonProperty("domain")]
    public string domain { get; set; }

    [JsonProperty("online_app")]
    public string online_app { get; set; }

    [JsonProperty("online_mobile")]
    public string online_miobile { get; set; }
   }

这里我从服务器加载数据

   var friend = vkUserInf.Load(friends);
   var jsonArray = JsonConvert.DeserializeObject<FriendsData>(friend);

毕竟我得到了 null

这是我的 JSON,来自可变朋友

"{\"响应\":{\"count\":109,\"items\":[{\"id\":105960511,\"first_name\":\"Anna\",\" last_name\":\"Vladimirovna\",\"domain\":\"pioneer_ann\",\"online\":1,\"online_app\":\"2274003\",\"online_mobile\":1, \"hidden\":1},{\"id\":209391889,\"first_name\":\"Bernard\",\"last_name\":\"Bouhidel\",\"domain\":\" id209391889\",\"在线\":0,\"隐藏\":1},{\"id\":220035155,\"first_name\":\"Damian\",\"last_name\":\" Wolkowski\",\"domain\":\"ijust_rock_and_no_more\",\"online\":1,\"online_app\":\"2274003\",\"online_mobile\":1}]}}"

如果我更改请求字符串并且在 JSON 中仅保留项目数组,我可以轻松反序列化它,但是对于这个字段我不知道

【问题讨论】:

  • 请发布一个有效的 json 字符串以便测试
  • 在您提供的内容中,有一些非打印字符会阻止正确的 json 解析。清除转义斜线并将您的 json 粘贴到 jsonlint.com 中,您将看到问题出在哪里。
  • 您的 JSON 属性与您的 C# 属性不匹配。此外,只需搜索您的标题即可找到多个匹配项;这些都没有帮助你吗?
  • 我已经通过添加属性解决了我的问题 public class FriendsData{ [JsonProperty("response")] public Friendfriend { get;放; } }

标签: .net json


【解决方案1】:

主要问题是在这个类中

public class FriendsData
{
    //[JsonProperty("response")]
    public Friend friend { get; set; }
}

我已经退出了我已经评论过的财产

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 2023-03-12
    • 2017-07-05
    • 1970-01-01
    • 2021-08-28
    相关资源
    最近更新 更多