[英]Unable to parse JSON Data with a list of dictionary
I want to parse JSON Data into C#我想将 JSON 数据解析为 C#
The data is:数据是:
"{\"data\":
{\"6\":{\"id\":\"6\",\"reward_definition_id\":12,\"person_id\":164305,\"issue_date\":\"2015-10-08\",\"expiry_date\":\"2015-11-08\",\"amount_initial\":20,\"amount_remaining\":0,\"
_member_details\":{\"id\":\"164305\",\"first_name\":\"test\",\"last_name\":\"one\",\"email\":\"testing1@example.com\",\"_email_trigger\":\"Y\",\"_email_bounced\":\"N\",\"_email_promo\":\"Y\",\"_app_notify\":\"Y\",\"_sms_status\":\"Y\",\"active\":\"Y\"},
\"_store_details\":null,\"_staff_details\":null,\"_product_details\":null},
\"5\":{\"id\":\"5\",\"reward_definition_id\":12,\"person_id\":164305,\"issue_date\":\"2015-10-08\",\"expiry_date\":\"2015-11-08\",\"amount_initial\":20,\"amount_remaining\":20,\"
_member_details\":{\"id\":\"164305\",\"first_name\":\"test\",\"last_name\":\"one\",\"email\":\"testing1@example.com\",\"_email_trigger\":\"Y\",\"_email_bounced\":\"N\",\"_email_promo\":\"Y\",\"_app_notify\":\"Y\",\"_sms_status\":\"Y\",\"active\":\"Y\"},
\"_store_details\":null,\"_staff_details\":null,\"_product_details\":null}
}}"
How to create a Class with Dictionary of lists of this data and deserialize it into C# class.如何使用此数据列表的字典创建一个类并将其反序列化为 C# 类。 I am trying to use the following classes to store this json data, the classes are我正在尝试使用以下类来存储此 json 数据,这些类是
public class JsonRewardResponse
{
public Dictionary<int, JsonRewardDetails> mydictionary { get; set; }
}
public class JsonRewardDetails
{
public string Id;
public int reward_definition_id;
public int person_id;
public string issue_date;
public string expiry_date;
public int amount_initial;
public int amount_remaining;
public JsonMemberDetails member;`enter code here`
public int _store_details;
public int _staff_details;
public int product_details;
}
public class JsonMemberDetails
{
public string Id;
public string first_name;
public string last_name;
public string email;
public string _email_trigger;
public string _email_bounced;
public string _email_promo;
public string _app_notify;
public string _sms_status;
public string active;
}
Please help Thanks in Advance.请帮助提前致谢。
Bharath AK巴拉特 AK
You don't say what serializer you are using, so the advice I am giving is relevant for for all JSON serializers.您没有说明您使用的是什么序列化程序,因此我给出的建议与所有 JSON 序列化程序相关。
You have a few problems here:你在这里有几个问题:
The name of the property mydictionary
should be data
, as that's what is in the JSON.属性mydictionary
的名称应该是data
,因为这是 JSON 中的内容。
product_details
should be renamed to _product_details
. product_details
应重命名为_product_details
。
_store_details
, _staff_details
and _product_details
are declared to be int
, however they appear as null
in the JSON. _store_details
、 _staff_details
和_product_details
被声明为int
,但它们在 JSON 中显示为null
。 Thus they need to be some sort of nullable type in c#, for instance int?
因此它们需要在 c# 中是某种可空类型,例如int?
or string
.或string
。 (The fact that these are null in the JSON suggests, but does not prove, that they are not integer types when non-null.) (这些在 JSON 中为 null 的事实表明,但并不能证明它们在非 null 时不是整数类型。)
member
should be renamed to _member_details
. member
应重命名为_member_details
。
Thus:因此:
public class JsonRewardResponse
{
public Dictionary<int, JsonRewardDetails> data { get; set; }
}
public class JsonRewardDetails
{
public string Id;
public int reward_definition_id;
public int person_id;
public string issue_date;
public string expiry_date;
public int amount_initial;
public int amount_remaining;
public JsonMemberDetails member;
public int? _store_details;
public int? _staff_details;
public int? _product_details;
}
public class JsonMemberDetails
{
public string Id;
public string first_name;
public string last_name;
public string email;
public string _email_trigger;
public string _email_bounced;
public string _email_promo;
public string _app_notify;
public string _sms_status;
public string active;
}
With these fixes, using Json.NET , you can do:通过这些修复程序,使用Json.NET ,您可以执行以下操作:
var response = JsonConvert.DeserializeObject<JsonRewardResponse>(json);
(You might have additional issues with other serializers.) (您可能会遇到其他序列化程序的其他问题。)
When I want to deserialize JSON, I either use the dynamic type in c# or if I want todo what you are doing I feed the json into the web site:当我想反序列化 JSON 时,我要么使用 c# 中的动态类型,要么如果我想做你正在做的事情,我将 json 输入到网站中:
http://json2csharp.com/ http://json2csharp.com/
I've never had an issue with that.我从来没有遇到过这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.