无法序列化 json 键值对
Unable to serialize json keyvaluepair
我有 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
创建 class 的 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; }
}
Only LocalInfo
, ServiceMap
and PriceMap
This properties are getting Null values. Everything apart it is serialized.
请告诉我我做错了什么
导致您的问题的原因是属性的名称。它们不能在 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(我以前不知道;真是个好工具!)在生成以下内容时给您提示 class:
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 生成的 class 之类的 PriceMap
等,并且可以像您已经使用的那样坚持使用 dynamic
在你的问题中。
基本上,发生的问题只是 属性 的名称。结果始终为空,因为您通过 DataMember
属性告诉 json.net 它应该将 json 键 "PriceMap"
映射到名为 [=17 的 属性 =].这不会起作用,因为您的 json 中没有 json 键 "PriceMap"
。相反,它被简单地称为 "price_map"
。所以删除 DataMember
属性,属性 将被正确映射。
附带说明:有一个很酷的功能 paste JSON as classes 自动生成必要的 classes你,如果你不想离开Visual Studio:
我有 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
创建 class 的 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; }
}
Only
LocalInfo
,ServiceMap
andPriceMap
This properties are getting Null values. Everything apart it is serialized.
请告诉我我做错了什么
导致您的问题的原因是属性的名称。它们不能在 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(我以前不知道;真是个好工具!)在生成以下内容时给您提示 class:
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 生成的 class 之类的 PriceMap
等,并且可以像您已经使用的那样坚持使用 dynamic
在你的问题中。
基本上,发生的问题只是 属性 的名称。结果始终为空,因为您通过 DataMember
属性告诉 json.net 它应该将 json 键 "PriceMap"
映射到名为 [=17 的 属性 =].这不会起作用,因为您的 json 中没有 json 键 "PriceMap"
。相反,它被简单地称为 "price_map"
。所以删除 DataMember
属性,属性 将被正确映射。
附带说明:有一个很酷的功能 paste JSON as classes 自动生成必要的 classes你,如果你不想离开Visual Studio: