2016-04-16 66 views
0

我有json,試圖使用Newtonsoft.Json將json去deseralize,但我無法獲得列表中的某些值。無法序列化json keyvaluepair

這是我的JSON看起來怎麼樣

{ 
    "status": "success", 
    "msg": "success", 
    "code": 1, 
    "data": { 
     "country": "India", 
     "countryid": "766", 
     "operator": "Airtel India", 
     "operatorid": "1371", 
     "connection_status": "99", 
     "destination_msisdn": "919895070723", 
     "destination_currency": "INR", 
     "product_list": "100,200,300,330,350,440,500,1000", 
     "service_fee_list": "0.00,0.00", 
     "retail_price_list": "7.50,14.50,21.50,70.50", 
     "wholesale_price_list": "6.05,12.03,29.97,59.85", 
     "local_info_value_list": "100.00,1000.00", 
     "local_info_amount_list": "87.00,175.00,887.00", 
     "local_info_currency": "INR", 
     "authentication_key": "16809", 
     "error_code": "0", 
     "error_txt": "Transaction successful", 
     "nick_name": "eldho", 
     "price_map": { 
      "100": "7.50", 
      "200": "14.50", 
      "300": "21.50", 
      "330": "23.50", 
      "350": "25.00", 
      "440": "31.50", 
      "500": "35.50", 
      "1000": "70.50" 
     }, 
     "local_info": { 
      "100": "87.00", 
      "200": "175.00", 
      "300": "264.00", 
      "330": "290.40", 
      "350": "350.00", 
      "440": "387.20", 
      "500": "442.00", 
      "1000": "887.00" 
     }, 
     "service_map": { 
      "100": "0.00", 
      "200": "0.00", 
      "300": "0.00", 
      "330": "0.00", 
      "350": "0.00", 
      "440": "0.00", 
      "500": "0.00", 
      "1000": "0.00" 
     }, 
     "operator_logo": "https:\/\/fm.transfer-to.com\/logo_operator\/logo-1371", 
     "promo_code": 0, 
     "benef_id": "16598" 
    } 
} 

更新:這local_info和service_map是動態的數據

我通常使用Json2charp

我seralization代碼爲創建一流的JSON模式像這樣

using (var _client = new HttpClient())   
{   
    _client.BaseAddress = new Uri(baseServiceUri); 

    var content = new FormUrlEncodedContent(new[] 
    { 
     new KeyValuePair<string,string>("someparameter",token), 
     new KeyValuePair<string,string>("id",myId.ToString()),   
    }); 

    var response = await _client.PostAsync(new Uri(baseAccessPoint, UriKind.Relative), content); 

    if (!response.IsSuccessStatusCode) 
    { 
     throw new HttpRequestException(response.ReasonPhrase); 
    } 

    //I get value in this 
    var responseResult = await response.Content.ReadAsStringAsync(); 

    //Unable to deseralize it. 
    return JsonConvert.DeserializeObject<BeneficoryMobileDTO>(responseResult, new JsonSerializerSettings() 
    { 
     //NullValueHandling = NullValueHandling.Ignore, 
     Error = JsonDeserializeErrorHandler,  
    }); 
} 

我的模型看起來像這樣。

[DataContract] 
public class BeneficoryMobileDTO 
{ 
    [DataMember] 
    public string status { get; set; } 

    [DataMember] 
    public string msg { get; set; } 

    [DataMember] 
    public string code { get; set; } 

    [DataMember(Name = "data")] 
    public BeneficoryMobileDetailsDTO BeneficoryMobileDetails { get; set; } 
} 

public class BeneficoryMobileDetailsDTO 
{ 
    //I have removed some property to show things that doesn't work 

    //THIS CAN'T BE SERALIZED 
    [DataMember(Name = "PriceMap")] 
    public dynamic price_map { get; set; } 

    //THIS CAN'T BE SERALIZED 
    [DataMember(Name = "ServiceMap")] 
    public dynamic service_map { get; set; } 

    //THIS CAN'T BE SERALIZED 
    [DataMember(Name = "LocalInfo")] 
    public object LocalInfo { get; set; } 


    [DataMember] 
    public int benef_id { get; set; } 
} 

只有LocalInfoServiceMapPriceMap這個特性越來越Null值。所有的東西都是連續的。

請讓我知道我做錯了

+0

數字總是相同的(即100,200,300,330等)? – DavidG

+0

那些是動態的 – Eldho

回答

0

原因你的問題是什麼屬性的名稱。它們不能直接在C#中用作標識符mustn't begin with a number (identifier-start-character)。所以像這樣的屬性是無效的:

public string 100 { get; set; } // "100" is an invalid property name 
// generates compiler error CS1519: Invalid token '100' in class, struct, or interface member declaration 

json2csharp(我以前不知道,真是一個不錯的工具!)給你一個暗示,因爲它產生了以下類:

public class PriceMap 
{ 
    public string __invalid_name__100 { get; set; } // invalid hint 
    public string __invalid_name__200 { get; set; } 
    // other props omitted 
} 

什麼你可以做,讓反序列化成功被標註這些屬性(請參閱documentation),並告訴他們json.net應該如何映射:

public class PriceMap 
{ 
    [JsonProperty(PropertyName = "100")] 
    public string _100 { get; set; } 
    [JsonProperty(PropertyName = "200")] 
    public string _200 { get; set; } 
    // other props omitted 
} 

備選:

你不必使用類,如PriceMap等通過json2csharp生成的,並且可以與dynamic堅持,你已經在你的問題一樣。

基本上發生的問題只是屬性的名稱。結果始終爲空,因爲您通過DataMember告知json.net - 它應該將json鍵"PriceMap"映射到名爲price_map的屬性。這不會工作,因爲json中沒有json鍵"PriceMap"。相反,它簡稱爲"price_map"。所以刪除DataMember-attribute,屬性將被正確映射。


而作爲一個側面說明:有一個很酷的功能貼JSON作爲類其中自動生成必要的類適合你,如果你不想離開的Visual Studio:

enter image description here

+0

這是動態生成的,所以我如何映射這個 – Eldho

+0

命名是問題。我的錯誤我以前沒有找到。 – Eldho

+0

請用相同的方式更新您的答案,我可以將其標記爲答案 – Eldho