2013-12-12 99 views
0

我正在研究從JSON格式的Battlelog API中收集數據的應用程序。 這是「玩家」類的樣子在JSONL:JsonConvert.DeserializeObject <Object>(字符串)不返回任何東西

{ 
    "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); 

然而,當我嘗試運行它只是不返回任何的方法,它留下的所有領域的空白,即使我沒有得到任何例外或錯誤。

我希望這是我錯過的小而簡單的事情,但我無法弄清楚什麼。

感謝提前:)

+0

那是確切的JSON你的要求得到什麼?因爲它缺少一個關閉'}'。我不認爲這是問題,但至少應該檢查一下。 –

+1

@Chris Mantle他說這只是一個粘貼錯誤,這就是我刪除我的答案的原因:) – albusshin

回答

2

您可以使用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; } 
} 
+0

這正是我之前用來創建類定義的網站,所以我不認爲這可能是它。 – user2722107

+1

@ user2722107您反序列化爲'Player'對象。看到答案,我用'RootObject' –

+0

對不起,我錯過了,它工作了!非常感謝你! – user2722107