【问题标题】:JsonConvert.DeserializeObject<Object>(string) doesnt return anythingJsonConvert.DeserializeObject<Object>(string) 不返回任何内容
【发布时间】:2013-12-12 14:52:05
【问题描述】:

我正在开发一个以 JSON 格式从 Battlelog API 收集数据的应用程序。 这是 JSONL 中“播放器”类的样子:

{
   "player":{
      "id":173521680,
      "game":"bf4",
      "plat":"pc",
      "name":"1ApRiL",
      "tag":"",
      "dateCheck":1386842250248,
      "dateUpdate":1386827475171,
      "dateCreate":1383962204725,
      "lastDay":"20131206",
      "country":"DE",
      "countryName":"Germany",
      "rank":{
         "nr":9,
         "imgLarge":"bf4/ranks/r9.png",
         "img":"r9",
         "name":"Lance Corporal IV",
         "needed":140000,
         "next":{
            "nr":10,
            "img":"r10",
            "name":"Lance Corporal V",
            "needed":168000,
            "curr":165309,
            "relNeeded":28000,
            "relCurr":25309,
            "relProg":90.38928571428572
         }
      },
      "score":165309,
      "timePlayed":25740,
      "uId":"2832660339132815718",
      "uName":"1ApRiL",
      "uGava":"7222ff803a0a67404aa082b22ff3fa5b",
      "udCreate":1319474914000,
      "blPlayer":"http://battlelog.battlefield.com/bf4/soldier/1ApRiL/stats/173521680/pc/",
      "blUser":"http://battlelog.battlefield.com/bf4/user/1ApRiL/",
      "editable":false,
      "viewable":true,
      "adminable":false,
      "linked":false
   }
}

这就是我的 C# 类 Player 的样子:

public class Player
{
    public int id { get; set; }
    public string game { get; set; }
    public string plat { get; set; }
    public string name { get; set; }
    public string tag { get; set; }
    public long dateCheck { get; set; }
    public long dateUpdate { get; set; }
    public long dateCreate { get; set; }
    public string lastDay { get; set; }
    public string country { get; set; }
    public object countryName { get; set; }
    public Rank rank { get; set; }
    public int score { get; set; }
    public int timePlayed { get; set; }
    public string uId { get; set; }
    public string uName { get; set; }
    public string uGava { get; set; }
    public long udCreate { get; set; }
    public string blPlayer { get; set; }
    public string blUser { get; set; }
    public bool editable { get; set; }
    public bool viewable { get; set; }
    public bool adminable { get; set; }
    public bool linked { get; set; }
}

最后:这就是我尝试反序列化它的方式(数据是 JSON 数据):

Player p = JsonConvert.DeserializeObject<Player>(data);

但是,当我尝试运行该方法时,它不会返回任何内容,它会将所有字段留空,即使我没有收到任何异常或错误。

我希望这是我错过的小而简单的东西,但我就是不知道是什么。

提前致谢:)

【问题讨论】:

  • 这是您从请求中获得的确切 JSON 吗?因为它缺少一个结束}。我不认为这是问题所在,但至少需要检查一下。
  • @Chris Mantle 他说这只是一个粘贴错误,这也是我删除答案的原因:)

标签: c# .net json api


【解决方案1】:

您可以使用this site 获取正确的类定义

var root  = JsonConvert.DeserializeObject<RootObject>(data);

public class Next
{
    public int nr { get; set; }
    public string img { get; set; }
    public string name { get; set; }
    public int needed { get; set; }
    public int curr { get; set; }
    public int relNeeded { get; set; }
    public int relCurr { get; set; }
    public double relProg { get; set; }
}

public class Rank
{
    public int nr { get; set; }
    public string imgLarge { get; set; }
    public string img { get; set; }
    public string name { get; set; }
    public int needed { get; set; }
    public Next next { get; set; }
}

public class Player
{
    public int id { get; set; }
    public string game { get; set; }
    public string plat { get; set; }
    public string name { get; set; }
    public string tag { get; set; }
    public long dateCheck { get; set; }
    public long dateUpdate { get; set; }
    public long dateCreate { get; set; }
    public string lastDay { get; set; }
    public string country { get; set; }
    public string countryName { get; set; }
    public Rank rank { get; set; }
    public int score { get; set; }
    public int timePlayed { get; set; }
    public string uId { get; set; }
    public string uName { get; set; }
    public string uGava { get; set; }
    public long udCreate { get; set; }
    public string blPlayer { get; set; }
    public string blUser { get; set; }
    public bool editable { get; set; }
    public bool viewable { get; set; }
    public bool adminable { get; set; }
    public bool linked { get; set; }
}

public class RootObject
{
    public Player player { get; set; }
}

【讨论】:

  • 这正是我之前用来创建类定义的网站,所以我认为不可能是这样。
  • @user2722107 你反序列化为Player 对象。看答案,我用RootObject
  • 对不起,我错过了,它有效!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 2019-04-06
  • 2012-02-15
  • 2020-09-07
  • 2021-11-22
  • 2015-11-06
  • 2013-11-14
相关资源
最近更新 更多