我有json,试图使用Newtonsoft.Json
对json进行解除分类,但我无法在列表中获得一些值。
这就是我的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
创建json模型类我的seralization代码就像这样
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; }
}
仅
LocalInfo
,ServiceMap
和PriceMap
此属性获取Null值。除了它之外的所有东西都是序列化的。
请让我知道我做错了什么
答案 0 :(得分: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
}
<强>替代:强>
您不必使用由json2csharp生成的PriceMap
等类,并且可以像您在问题中所做的那样坚持使用dynamic
。
基本上,发生的问题只是属性的名称。结果始终为null,因为您通过DataMember
- 属性告诉json.net它应该将json键"PriceMap"
映射到名为price_map
的属性。这不会起作用,因为你的json中没有json键"PriceMap"
。相反,它只是简称为"price_map"
。因此,删除DataMember
- 属性,将正确映射属性。
作为旁注:如果您不想离开Visual Studio,那么将JSON粘贴为类会为您自动生成必要的类,这是一个很酷的功能: