2014-10-08 115 views
0

我想反序列化來自battle.net apis的JSON對象,我找不出將json對象放入其中的類的格式。將json對象數組解析爲一個對象

在運行期間,「Races」數組始終爲空。

我正在使用相同的請求從同一服務收集數據,所以它不是其餘的請求多數民衆贊成在這個問題。

我使用C#與RestSharp庫

這裏是我反序列化JSON:

{ 
    "races": [{ 
     "id": 6, 
     "mask": 32, 
     "side": "horde", 
     "name": "Tauren" 
    }, { 
     "id": 5, 
     "mask": 16, 
     "side": "horde", 
     "name": "Undead" 
    }, { 
     "id": 2, 
     "mask": 2, 
     "side": "horde", 
     "name": "Orc" 
    }, { 
     "id": 7, 
     "mask": 64, 
     "side": "alliance", 
     "name": "Gnome" 
    }, { 
     "id": 9, 
     "mask": 256, 
     "side": "horde", 
     "name": "Goblin" 
    }, { 
     "id": 1, 
     "mask": 1, 
     "side": "alliance", 
     "name": "Human" 
    }, { 
     "id": 8, 
     "mask": 128, 
     "side": "horde", 
     "name": "Troll" 
    }, { 
     "id": 24, 
     "mask": 8388608, 
     "side": "neutral", 
     "name": "Pandaren" 
    }, { 
     "id": 11, 
     "mask": 1024, 
     "side": "alliance", 
     "name": "Draenei" 
    }, { 
     "id": 22, 
     "mask": 2097152, 
     "side": "alliance", 
     "name": "Worgen" 
    }, { 
     "id": 10, 
     "mask": 512, 
     "side": "horde", 
     "name": "Blood Elf" 
    }, { 
     "id": 4, 
     "mask": 8, 
     "side": "alliance", 
     "name": "Night Elf" 
    }, {  
     "id": 3, 
     "mask": 4, 
     "side": "alliance", 
     "name": "Dwarf" 
    }, { 
     "id": 25, 
     "mask": 16777216, 
     "side": "alliance", 
     "name": "Pandaren" 
    }, { 
     "id": 26, 
     "mask": 33554432, 
     "side": "horde", 
     "name": "Pandaren" 
    }] 
} 

,這裏是我試圖反序列化到類:

using System; 
using System.Collections.Generic; 

namespace RESTTests 
{ 

    public class Race 
    { 

     public int Id { get; set; } 


     public int Mask { get; set; } 


     public string Side { get; set; } 


     public string Name { get; set; } 
    } 

    public class RaceData 
    { 

     public Race[] Races { get; set; } 
    } 

} 

編輯:更多代碼請求

public void GetRaceData() 
    { 
     var client = CreateClient(); 
     var request = CreateRequest("wow/data/character/classes"); 

     IRestResponse<RaceData> responseDe = client.Execute<RaceData>(request); 
     RaceData race = responseDe.Data; 

     //at this point "races" in the race object is null and races in the response.Data object are also null 

    } 

    private RestRequest CreateRequest(String segement) 
    { 
     var request = new RestRequest(segement, Method.GET); 
     request.AddParameter("locale", _locale); 
     request.AddParameter("apikey", _apiKey); 
     return request; 
    } 

    private RestClient CreateClient() 
    { 
     var client = new RestClient("https://us.api.battle.net/"); 
     return client; 
    } 
+0

你真正的反序列化代碼在哪裏?你模型班看起來很好。 – 2014-10-08 17:51:28

+0

在串行器中可能區分大小寫。嘗試更改輸入或類中屬性的第一個字符。 – Adam47 2014-10-08 17:58:29

+0

@MattBurland我用我的序列化代碼編輯了OP – 2014-10-08 19:03:23

回答

2

你可以嘗試使用Newtonsoft.Json,樣品:

string json = "load your json here as a string"; 

Race race = JsonConvert.DeserializeObject<Race>(json); 

我覺得RestSharp lib中已經有了Json.NET作爲依賴引用的,所以,你可以從那裏使用它。

+1

他們去年從RestSharp中刪除了Newtonsoft引用我想:https://github.com/restsharp/RestSharp/blob/master/readme.txt – 2014-10-08 19:05:08

+0

很高興看到,但你可以從'NuGet'中添加:) – 2014-10-08 19:08:04

2

你有這個類:

public class Race 
{ 
    public int id { get; set; } 
    public int mask { get; set; } 
    public string side { get; set; } 
    public string name { get; set; } 
} 

public class RootObject 
{ 
    public List<Race> races { get; set; } 
} 

你總是可以使用json2csharp.com得到生成的C#類JSON反對。

+0

試過,數組在運行時仍爲空。我將我的序列化代碼附加到OP – 2014-10-08 19:05:51