JSON 来自 PipeDrive API 的结果未反序列化
JSON result from PipeDrive API does not deserialize
抱歉拖了这么久post,事实上我不知道问题可能会持续在哪里。
我正在开发一个程序,需要使用自定义字段从 PipeDrive 中提取与选定组织相关的交易,进行一些计算并将结果保存为文档。目前它执行以下操作:
- 查询 API 使用文本框输入作为关键字的公司。
- 在使用 JSON2CSharp.
生成的 class 中反序列化返回的 JSON
- 将结果放入列表。
我已经把这部分弄下来了,它工作得很好。然而,当我计算出需求的第二部分时,事情变得很奇怪。步骤我会细说的:
单击组织后,查询 API 与组织 ID 和名称对应的交易。
public List<int> GetOrgDealcodes(string ID, string search)
{
List<int> returnlist = new List<int>();
JsonTextReader reader = new JsonTextReader(new StringReader((string)ReturnObjectsByName(PipeLineNiveau.deals, search, ID)));
reader.SupportMultipleContent = true;
JsonSerializer serializer = new JsonSerializer();
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
try
{
PipeDriveDeals.RootObject root = serializer.Deserialize<PipeDriveDeals.RootObject>(reader);
foreach (PipeDriveDeals.data tmp in root.data)
{
returnlist.Add(tmp.id);
}
}
catch (JsonException ex)
{
Error.TopBar("Deserializing error", ex.Message);
}
return returnlist;
}
ReturnObjectsByName()
returns 包含 JSON 响应的 object
获取每个返回的交易 ID 的详细信息。
public PipeDriveDeals.data GetDeal(string ID)
{
PipeDriveDeals.data returnvar = new PipeDriveDeals.data();
JsonTextReader reader = new JsonTextReader(new StringReader((string)ReturnObject(PipeLineNiveau.deals, ID)));
reader.SupportMultipleContent = true;
JsonSerializer serializer = new JsonSerializer();
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
try
{
returnvar = serializer.Deserialize<PipeDriveDeals.data>(reader);
}
catch(Exception ex)
{
Error.TopBar("Error reading deal", ex.ToString());
}
return returnvar;
}
ReturnObject()
returns 包含 JSON 响应的 object
反序列化返回的 JSON 在 class 中用 JSON2CSharp 生成(在 GetDeal() 中显示),意思是:
{
"success": true,
"data": {
"id": 1,
"user_id": {
"id": [censored],
"name": "[censored]",
"email": "administratie@[censored]",
"has_pic": false,
"pic_hash": null,
"value": [censored]
},
"person_id": {
"name": "[censored]",
"email": "[censored]@[censored]",
"phone": "[censored]",
"value": 1
},
"org_id": {
"name": "Test",
"people_count": 1,
"cc_email": "[censored]com2@pipedrivemail.com",
"value": 1
},
"stage_id": 13,
"title": "Test deal",
"value": 200,
"currency": "EUR",
"add_time": "2015-04-23 13:40:42",
"update_time": "2015-05-21 15:34:39",
"stage_change_time": "2015-04-29 08:29:35",
"active": false,
"deleted": true,
"status": "deleted",
"next_activity_date": null,
"next_activity_time": null,
"next_activity_id": null,
"last_activity_id": null,
"last_activity_date": null,
"lost_reason": null,
"visible_to": "3",
"close_time": "2015-05-21 15:34:39",
"pipeline_id": 2,
"won_time": null,
"lost_time": null,
"products_count": 2,
"files_count": 1,
"notes_count": 1,
"followers_count": 1,
"email_messages_count": null,
"activities_count": null,
"done_activities_count": null,
"undone_activities_count": null,
"reference_activities_count": null,
"participants_count": 1,
"expected_close_date": null,
"f97e4841747d1fc621809ebbd0063f52f4eecf74": "Person",
"052d4ce58edf16df90dab070591bd070f8ad88e7": "27-02-1992",
"28fb37b17020f6dbd45ee92c94ae34811e94edd8": "Company",
"a1032d833c7cba5a18ca09cd5418bfa3fd919137_currency": "EUR",
"d7c2cb45d01c81159dd684111aa41bbbe5ce6db5_currency": "EUR",
"d54e4d7b41a5bdfb2a29f7d57d8c1a4fd2ece115_currency": "EUR",
"267976bb28be655ecf0bed63efbf4822b75aa0cf_currency": "EUR",
"28e42860f533c6837224736257b963f3d8418ad1": "Company",
"211a67285816fc5179c248ef08e52000a2d1b15b_currency": "EUR",
"1697fe1baa42670ccebdb863bc03729b56d8857d_currency": "EUR",
"4dc8edc835c39dbe422f3e1dd14f2fa3072866a8_currency": "EUR",
"54dce1f16a984fa7d62e9c31c4c09d8832c0e423_currency": "EUR",
"8985469f141b2df5f3236947983feea015d24145": "3,4,5",
"05a20487e9aa7a069f678aa2af92be5c75675416": null,
"275ac9794b3f274c1c61b47033b298d94a0125fd": null,
"24363789868fc8fada2400a3644cb11f7683df04": null,
"0670e6a82e67e19166f11c11778229d783395d0f": null,
"3b0feefce60bc79e9d9fb15bc20c57a05e0ebff8": null,
"d3748987f13e9dcdd60ffc6a567d3fd80431d77f": null,
"c4fbdf6431406db25949cdd90dfc91eeb5295165": null,
"c357146d49b62fe08d34bbc98550b2b96431f773": null,
"91bd07ed77e652cf6ac4a8b82c83e7e9932dbff3": null,
"235d277ff53390255c6c505a1634c88646fa3748": null,
"5c06d93f2b2ef968478235270d24c91b90cd857d": null,
"0b8350fa9af813124cf6d039ef2c932297fb1b49": null,
"547d1d42798933363859853e775d42974080a291": null,
"08610b715244a8ddfb3aeb1b4585c4487afd67bd": null,
"88ea9aadb49d3b4da1d17d2224e6d4b82a532a40": null,
"49eba2416e95c88ac3ec279a1534884429872d21": null,
"817f3b1c7ac9220fca3a9275926dabdfb914d885": null,
"206f941b5d992f43d5123b2a42c34db758ecc826": null,
"60a22e72ff54fa91b04376d6c373f35da19d94ba_currency": null,
"1326c4f84ec8611a202cc58a66d091617b910b4d": null,
"e13d704375bb3ee3c4523ba2e1e2f79779329615": null,
"d9bc80190727512287a4951e160b0f81d8b8f227": null,
"7cafce1dd6b89b14f69c23d9f1f56e8f67a8c00b": null,
"a451543d82d05cd98464d2c8b4141f95038e7e55": null,
"67fcb1f393b0bff6c83d43414cd432b695a4d9c2": null,
"3776ab9561a6f50d3becfd63aae885a3f9a6ce86": null,
"0f3c7cb8eda95298a7e17cc4c5b6aea1fed99476": null,
"b5039fda6e758955f6aefcdf86bc654fd2e7d8bd": null,
"5c194672d7994903077944047cec441673e7b1e6": null,
"f04a5560d8e408620c633230e141bf16a6874b30": null,
"2502aab37117657e5fe04c2ce688b80b8e7aa17f": null,
"ef704e3b5bc356ebceb81f00628e5813b8ecf2e5": null,
"f4aaa546fb4bc886aed645789ccaaebad4fe60d8": null,
"eefb3dbc53aa0d99ef0a57a864af562f6f6de49a": null,
"5144ea977b5678f238d5aa11418510c18efcf4fc": null,
"stage_order_nr": 5,
"person_name": "Person",
"org_name": "Test",
"next_activity_subject": null,
"next_activity_type": null,
"next_activity_duration": null,
"next_activity_note": null,
"formatted_value": "€ 200",
"weighted_value": 190,
"formatted_weighted_value": "€ 190",
"rotten_time": null,
"owner_name": "[censored]",
"cc_email": "[censored]com2+deal1@pipedrivemail.com",
"org_hidden": false,
"person_hidden": false,
"average_time_to_won": {
"y": 0,
"m": 0,
"d": 0,
"h": 0,
"i": 0,
"s": 0,
"total_seconds": 0
},
"average_stage_progress": 0,
"age": {
"y": 0,
"m": 1,
"d": 17,
"h": 18,
"i": 18,
"s": 28,
"total_seconds": 4126708
},
"stay_in_pipeline_stages": {
"times_in_stages": {
"1": 435229,
"2": 0,
"3": 0,
"4": 63812,
"9": 692,
"10": 0,
"11": 0,
"12": 0,
"13": 3626975
},
"order_of_stages": [
11,
12,
13,
10,
9,
2,
3,
4,
1
]
},
"last_activity": null,
"next_activity": null
},
"additional_data": {
"dropbox_email": "[censored]com2+deal1@pipedrivemail.com"
},
"related_objects": {
"organization": {
"1": {
"id": 1,
"name": "Test",
"people_count": 1,
"cc_email": "[censored]com2@pipedrivemail.com"
}
},
"user": {
"[censored]": {
"id": [censored],
"name": "[censored]",
"email": "administratie@[censored]",
"has_pic": false,
"pic_hash": null
}
},
"person": {
"1": {
"id": 1,
"name": "Person",
"email": "[censored]@[censored]",
"phone": "[censored]"
}
}
}
}
对此:
class PipeDriveDeals
{
public class UserId
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public bool has_pic { get; set; }
public object pic_hash { get; set; }
public int value { get; set; }
}
public class PersonId
{
public string name { get; set; }
public string email { get; set; }
public object phone { get; set; }
public int value { get; set; }
}
public class OrgId
{
public string name { get; set; }
public int people_count { get; set; }
public string cc_email { get; set; }
public int value { get; set; }
}
public class __invalid_type__0f3c7cb8eda95298a7e17cc4c5b6aea1fed99476
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public bool has_pic { get; set; }
public string pic_hash { get; set; }
public int value { get; set; }
}
public class AverageTimeToWon
{
public int y { get; set; }
public int m { get; set; }
public int d { get; set; }
public int h { get; set; }
public int i { get; set; }
public int s { get; set; }
public int total_seconds { get; set; }
}
public class Age
{
public int y { get; set; }
public int m { get; set; }
public int d { get; set; }
public int h { get; set; }
public int i { get; set; }
public int s { get; set; }
public int total_seconds { get; set; }
}
public class TimesInStages
{
public int pref_11 { get; set; }
public int pref_12 { get; set; }
}
public class StayInPipelineStages
{
public TimesInStages times_in_stages { get; set; }
public List<int> order_of_stages { get; set; }
}
public class data
{
public int id { get; set; }
public int user_id { get; set; }
public int person_id { get; set; }
public int org_id { get; set; }
public int stage_id { get; set; }
public string title { get; set; }
public int value { get; set; }
public string currency { get; set; }
public string add_time { get; set; }
public string update_time { get; set; }
public string stage_change_time { get; set; }
public bool active { get; set; }
public bool deleted { get; set; }
public string status { get; set; }
public DateTime next_activity_date { get; set; }
public DateTime next_activity_time { get; set; }
public int next_activity_id { get; set; }
public int last_activity_id { get; set; }
public DateTime last_activity_date { get; set; }
public string lost_reason { get; set; }
public string visible_to { get; set; }
public DateTime close_time { get; set; }
public int pipeline_id { get; set; }
public DateTime won_time { get; set; }
public DateTime lost_time { get; set; }
public int products_count { get; set; }
public int files_count { get; set; }
public int notes_count { get; set; }
public int followers_count { get; set; }
public int email_messages_count { get; set; }
public int activities_count { get; set; }
public int done_activities_count { get; set; }
public int undone_activities_count { get; set; }
public int reference_activities_count { get; set; }
public int participants_count { get; set; }
public DateTime expected_close_date { get; set; }
[JsonProperty(PropertyName = "f97e4841747d1fc621809ebbd0063f52f4eecf74")]
public string f97e4841747d1fc621809ebbd0063f52f4eecf74 { get; set; }
[JsonProperty(PropertyName = "052d4ce58edf16df90dab070591bd070f8ad88e7")]
public string pref_052d4ce58edf16df90dab070591bd070f8ad88e7 { get; set; }
[JsonProperty(PropertyName = "28fb37b17020f6dbd45ee92c94ae34811e94edd8")]
public string pref_28fb37b17020f6dbd45ee92c94ae34811e94edd8 { get; set; }
[JsonProperty(PropertyName = "a1032d833c7cba5a18ca09cd5418bfa3fd919137_currency")]
public string a1032d833c7cba5a18ca09cd5418bfa3fd919137_currency { get; set; }
[JsonProperty(PropertyName = "d7c2cb45d01c81159dd684111aa41bbbe5ce6db5_currency")]
public string d7c2cb45d01c81159dd684111aa41bbbe5ce6db5_currency { get; set; }
[JsonProperty(PropertyName = "d54e4d7b41a5bdfb2a29f7d57d8c1a4fd2ece115_currency")]
public string d54e4d7b41a5bdfb2a29f7d57d8c1a4fd2ece115_currency { get; set; }
[JsonProperty(PropertyName = "267976bb28be655ecf0bed63efbf4822b75aa0cf_currency")]
public string pref_267976bb28be655ecf0bed63efbf4822b75aa0cf_currency { get; set; }
[JsonProperty(PropertyName = "28e42860f533c6837224736257b963f3d8418ad1")]
public string pref_28e42860f533c6837224736257b963f3d8418ad1 { get; set; }
[JsonProperty(PropertyName = "211a67285816fc5179c248ef08e52000a2d1b15b_currency")]
public string pref_211a67285816fc5179c248ef08e52000a2d1b15b_currency { get; set; }
[JsonProperty(PropertyName = "1697fe1baa42670ccebdb863bc03729b56d8857d_currency")]
public string pref_1697fe1baa42670ccebdb863bc03729b56d8857d_currency { get; set; }
[JsonProperty(PropertyName = "4dc8edc835c39dbe422f3e1dd14f2fa3072866a8_currency")]
public string pref_4dc8edc835c39dbe422f3e1dd14f2fa3072866a8_currency { get; set; }
[JsonProperty(PropertyName = "54dce1f16a984fa7d62e9c31c4c09d8832c0e423_currency")]
public string pref_54dce1f16a984fa7d62e9c31c4c09d8832c0e423_currency { get; set; }
[JsonProperty(PropertyName = "8985469f141b2df5f3236947983feea015d24145")]
public string pref_8985469f141b2df5f3236947983feea015d24145 { get; set; }
[JsonProperty(PropertyName = "05a20487e9aa7a069f678aa2af92be5c75675416")]
public string pref_05a20487e9aa7a069f678aa2af92be5c75675416 { get; set; }
[JsonProperty(PropertyName = "275ac9794b3f274c1c61b47033b298d94a0125fd")]
public string pref_275ac9794b3f274c1c61b47033b298d94a0125fd { get; set; }
[JsonProperty(PropertyName = "24363789868fc8fada2400a3644cb11f7683df04")]
public string pref_24363789868fc8fada2400a3644cb11f7683df04 { get; set; }
[JsonProperty(PropertyName = "0670e6a82e67e19166f11c11778229d783395d0f")]
public string pref_0670e6a82e67e19166f11c11778229d783395d0f { get; set; }
[JsonProperty(PropertyName = "3b0feefce60bc79e9d9fb15bc20c57a05e0ebff8")]
public string pref_3b0feefce60bc79e9d9fb15bc20c57a05e0ebff8 { get; set; }
[JsonProperty(PropertyName = "d3748987f13e9dcdd60ffc6a567d3fd80431d77f")]
public string d3748987f13e9dcdd60ffc6a567d3fd80431d77f { get; set; }
[JsonProperty(PropertyName = "c4fbdf6431406db25949cdd90dfc91eeb5295165")]
public string c4fbdf6431406db25949cdd90dfc91eeb5295165 { get; set; }
[JsonProperty(PropertyName = "c357146d49b62fe08d34bbc98550b2b96431f773")]
public int c357146d49b62fe08d34bbc98550b2b96431f773 { get; set; }
[JsonProperty(PropertyName = "91bd07ed77e652cf6ac4a8b82c83e7e9932dbff3")]
public string pref_91bd07ed77e652cf6ac4a8b82c83e7e9932dbff3 { get; set; }
[JsonProperty(PropertyName = "235d277ff53390255c6c505a1634c88646fa3748")]
public string pref_235d277ff53390255c6c505a1634c88646fa3748 { get; set; }
[JsonProperty(PropertyName = "5c06d93f2b2ef968478235270d24c91b90cd857d")]
public string pref_5c06d93f2b2ef968478235270d24c91b90cd857d { get; set; }
[JsonProperty(PropertyName = "0b8350fa9af813124cf6d039ef2c932297fb1b49")]
public string pref_0b8350fa9af813124cf6d039ef2c932297fb1b49 { get; set; }
[JsonProperty(PropertyName = "547d1d42798933363859853e775d42974080a291")]
public string pref_547d1d42798933363859853e775d42974080a291 { get; set; }
[JsonProperty(PropertyName = "08610b715244a8ddfb3aeb1b4585c4487afd67bd")]
public string pref_08610b715244a8ddfb3aeb1b4585c4487afd67bd { get; set; }
[JsonProperty(PropertyName = "88ea9aadb49d3b4da1d17d2224e6d4b82a532a40")]
public string pref_88ea9aadb49d3b4da1d17d2224e6d4b82a532a40 { get; set; }
[JsonProperty(PropertyName = "49eba2416e95c88ac3ec279a1534884429872d21")]
public string pref_49eba2416e95c88ac3ec279a1534884429872d21 { get; set; }
[JsonProperty(PropertyName = "817f3b1c7ac9220fca3a9275926dabdfb914d885")]
public string pref_817f3b1c7ac9220fca3a9275926dabdfb914d885 { get; set; }
[JsonProperty(PropertyName = "206f941b5d992f43d5123b2a42c34db758ecc826")]
public string pref_206f941b5d992f43d5123b2a42c34db758ecc826 { get; set; }
[JsonProperty(PropertyName = "60a22e72ff54fa91b04376d6c373f35da19d94ba_currency")]
public string pref_60a22e72ff54fa91b04376d6c373f35da19d94ba_currency { get; set; }
[JsonProperty(PropertyName = "1326c4f84ec8611a202cc58a66d091617b910b4d")]
public string pref_1326c4f84ec8611a202cc58a66d091617b910b4d { get; set; }
[JsonProperty(PropertyName = "e13d704375bb3ee3c4523ba2e1e2f79779329615")]
public string e13d704375bb3ee3c4523ba2e1e2f79779329615 { get; set; }
[JsonProperty(PropertyName = "d9bc80190727512287a4951e160b0f81d8b8f227")]
public string d9bc80190727512287a4951e160b0f81d8b8f227 { get; set; }
[JsonProperty(PropertyName = "7cafce1dd6b89b14f69c23d9f1f56e8f67a8c00b")]
public string pref_7cafce1dd6b89b14f69c23d9f1f56e8f67a8c00b { get; set; }
[JsonProperty(PropertyName = "a451543d82d05cd98464d2c8b4141f95038e7e55")]
public string a451543d82d05cd98464d2c8b4141f95038e7e55 { get; set; }
[JsonProperty(PropertyName = "67fcb1f393b0bff6c83d43414cd432b695a4d9c2")]
public string pref_67fcb1f393b0bff6c83d43414cd432b695a4d9c2 { get; set; }
[JsonProperty(PropertyName = "3776ab9561a6f50d3becfd63aae885a3f9a6ce86")]
public string pref_3776ab9561a6f50d3becfd63aae885a3f9a6ce86 { get; set; }
[JsonProperty(PropertyName = "0f3c7cb8eda95298a7e17cc4c5b6aea1fed99476")]
public __invalid_type__0f3c7cb8eda95298a7e17cc4c5b6aea1fed99476 pref_0f3c7cb8eda95298a7e17cc4c5b6aea1fed99476 { get; set; }
[JsonProperty(PropertyName = "b5039fda6e758955f6aefcdf86bc654fd2e7d8bd")]
public string b5039fda6e758955f6aefcdf86bc654fd2e7d8bd { get; set; }
[JsonProperty(PropertyName = "5c194672d7994903077944047cec441673e7b1e6")]
public string pref_5c194672d7994903077944047cec441673e7b1e6 { get; set; }
[JsonProperty(PropertyName = "f04a5560d8e408620c633230e141bf16a6874b30")]
public double f04a5560d8e408620c633230e141bf16a6874b30 { get; set; }
[JsonProperty(PropertyName = "2502aab37117657e5fe04c2ce688b80b8e7aa17f")]
public double pref_2502aab37117657e5fe04c2ce688b80b8e7aa17f { get; set; }
[JsonProperty(PropertyName = "ef704e3b5bc356ebceb81f00628e5813b8ecf2e5")]
public string ef704e3b5bc356ebceb81f00628e5813b8ecf2e5 { get; set; }
[JsonProperty(PropertyName = "f4aaa546fb4bc886aed645789ccaaebad4fe60d8")]
public string f4aaa546fb4bc886aed645789ccaaebad4fe60d8 { get; set; }
[JsonProperty(PropertyName = "5144ea977b5678f238d5aa11418510c18efcf4fc")]
public string pref_5144ea977b5678f238d5aa11418510c18efcf4fc { get; set; }
[JsonProperty(PropertyName = "eefb3dbc53aa0d99ef0a57a864af562f6f6de49a")]
public string pref_eefb3dbc53aa0d99ef0a57a864af562f6f6de49a { get; set; }
public int stage_order_nr { get; set; }
public string person_name { get; set; }
public string org_name { get; set; }
public string next_activity_subject { get; set; }
public string next_activity_type { get; set; }
public string next_activity_duration { get; set; }
public string next_activity_note { get; set; }
public string formatted_value { get; set; }
public int weighted_value { get; set; }
public string formatted_weighted_value { get; set; }
public string rotten_time { get; set; }
public string owner_name { get; set; }
public string cc_email { get; set; }
public bool org_hidden { get; set; }
public bool person_hidden { get; set; }
public AverageTimeToWon average_time_to_won { get; set; }
public int average_stage_progress { get; set; }
public Age age { get; set; }
public StayInPipelineStages stay_in_pipeline_stages { get; set; }
public string last_activity { get; set; }
public string next_activity { get; set; }
}
public class AdditionalData
{
public string dropbox_email { get; set; }
}
public class __invalid_type__509734
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public bool has_pic { get; set; }
public object pic_hash { get; set; }
}
public class User
{
public __invalid_type__509734 pref_509734 { get; set; }
}
public class __invalid_type__22964
{
public int id { get; set; }
public string name { get; set; }
public int people_count { get; set; }
public string cc_email { get; set; }
}
public class Organization
{
public __invalid_type__22964 pref_22964 { get; set; }
}
public class __invalid_type__30607
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public object phone { get; set; }
}
public class Person
{
public __invalid_type__30607 pref_30607 { get; set; }
}
public class RelatedObjects
{
public User user { get; set; }
public Organization organization { get; set; }
public Person person { get; set; }
}
public class RootObject
{
public bool success { get; set; }
public List<data> data { get; set; }
public AdditionalData additional_data { get; set; }
public RelatedObjects related_objects { get; set; }
}
}
- 在不同的 classes 中处理所有内容,以便计算和打印结果。 (此代码与问题无关)
在调试时我发现请求是正确的,返回的数据正是我所期望的(如第三步所示)并且在反序列化期间缓冲区被所述数据填充,但它 不是 用预期值填充 returnvar
class:
我不知道为什么会这样,毕竟我与 PipeDrive API 进行了另外两次交互,它们使用完全相同的方法,具有非常相似的实现,并且工作得很好。
我还注意到 PipeDrive 使用随机字符串作为自定义字段的标识。其中一些往往以数字开头,这意味着不可能创建具有相同名称的变量(因此 pref_
和 __invalid_type__
前缀)。我试图通过设置 JsonProperty
来解决这个问题。这可能是问题所在吗?我的解决方法还不够吗?
我来晚了,但 post 对于那些处理类似问题的人来说,答案似乎是公平的:事实证明这毕竟是一个菜鸟错误。这个错误的答案是我试图填写 data
class 而不是 RootObject
,这在给定层次结构的情况下是行不通的。
抱歉拖了这么久post,事实上我不知道问题可能会持续在哪里。
我正在开发一个程序,需要使用自定义字段从 PipeDrive 中提取与选定组织相关的交易,进行一些计算并将结果保存为文档。目前它执行以下操作:
- 查询 API 使用文本框输入作为关键字的公司。
- 在使用 JSON2CSharp. 生成的 class 中反序列化返回的 JSON
- 将结果放入列表。
我已经把这部分弄下来了,它工作得很好。然而,当我计算出需求的第二部分时,事情变得很奇怪。步骤我会细说的:
单击组织后,查询 API 与组织 ID 和名称对应的交易。
public List<int> GetOrgDealcodes(string ID, string search) { List<int> returnlist = new List<int>(); JsonTextReader reader = new JsonTextReader(new StringReader((string)ReturnObjectsByName(PipeLineNiveau.deals, search, ID))); reader.SupportMultipleContent = true; JsonSerializer serializer = new JsonSerializer(); serializer.ObjectCreationHandling = ObjectCreationHandling.Replace; try { PipeDriveDeals.RootObject root = serializer.Deserialize<PipeDriveDeals.RootObject>(reader); foreach (PipeDriveDeals.data tmp in root.data) { returnlist.Add(tmp.id); } } catch (JsonException ex) { Error.TopBar("Deserializing error", ex.Message); } return returnlist; }
ReturnObjectsByName()
returns 包含 JSON 响应的object
获取每个返回的交易 ID 的详细信息。
public PipeDriveDeals.data GetDeal(string ID) { PipeDriveDeals.data returnvar = new PipeDriveDeals.data(); JsonTextReader reader = new JsonTextReader(new StringReader((string)ReturnObject(PipeLineNiveau.deals, ID))); reader.SupportMultipleContent = true; JsonSerializer serializer = new JsonSerializer(); serializer.ObjectCreationHandling = ObjectCreationHandling.Replace; try { returnvar = serializer.Deserialize<PipeDriveDeals.data>(reader); } catch(Exception ex) { Error.TopBar("Error reading deal", ex.ToString()); } return returnvar; }
ReturnObject()
returns 包含 JSON 响应的object
反序列化返回的 JSON 在 class 中用 JSON2CSharp 生成(在 GetDeal() 中显示),意思是:
{ "success": true, "data": { "id": 1, "user_id": { "id": [censored], "name": "[censored]", "email": "administratie@[censored]", "has_pic": false, "pic_hash": null, "value": [censored] }, "person_id": { "name": "[censored]", "email": "[censored]@[censored]", "phone": "[censored]", "value": 1 }, "org_id": { "name": "Test", "people_count": 1, "cc_email": "[censored]com2@pipedrivemail.com", "value": 1 }, "stage_id": 13, "title": "Test deal", "value": 200, "currency": "EUR", "add_time": "2015-04-23 13:40:42", "update_time": "2015-05-21 15:34:39", "stage_change_time": "2015-04-29 08:29:35", "active": false, "deleted": true, "status": "deleted", "next_activity_date": null, "next_activity_time": null, "next_activity_id": null, "last_activity_id": null, "last_activity_date": null, "lost_reason": null, "visible_to": "3", "close_time": "2015-05-21 15:34:39", "pipeline_id": 2, "won_time": null, "lost_time": null, "products_count": 2, "files_count": 1, "notes_count": 1, "followers_count": 1, "email_messages_count": null, "activities_count": null, "done_activities_count": null, "undone_activities_count": null, "reference_activities_count": null, "participants_count": 1, "expected_close_date": null, "f97e4841747d1fc621809ebbd0063f52f4eecf74": "Person", "052d4ce58edf16df90dab070591bd070f8ad88e7": "27-02-1992", "28fb37b17020f6dbd45ee92c94ae34811e94edd8": "Company", "a1032d833c7cba5a18ca09cd5418bfa3fd919137_currency": "EUR", "d7c2cb45d01c81159dd684111aa41bbbe5ce6db5_currency": "EUR", "d54e4d7b41a5bdfb2a29f7d57d8c1a4fd2ece115_currency": "EUR", "267976bb28be655ecf0bed63efbf4822b75aa0cf_currency": "EUR", "28e42860f533c6837224736257b963f3d8418ad1": "Company", "211a67285816fc5179c248ef08e52000a2d1b15b_currency": "EUR", "1697fe1baa42670ccebdb863bc03729b56d8857d_currency": "EUR", "4dc8edc835c39dbe422f3e1dd14f2fa3072866a8_currency": "EUR", "54dce1f16a984fa7d62e9c31c4c09d8832c0e423_currency": "EUR", "8985469f141b2df5f3236947983feea015d24145": "3,4,5", "05a20487e9aa7a069f678aa2af92be5c75675416": null, "275ac9794b3f274c1c61b47033b298d94a0125fd": null, "24363789868fc8fada2400a3644cb11f7683df04": null, "0670e6a82e67e19166f11c11778229d783395d0f": null, "3b0feefce60bc79e9d9fb15bc20c57a05e0ebff8": null, "d3748987f13e9dcdd60ffc6a567d3fd80431d77f": null, "c4fbdf6431406db25949cdd90dfc91eeb5295165": null, "c357146d49b62fe08d34bbc98550b2b96431f773": null, "91bd07ed77e652cf6ac4a8b82c83e7e9932dbff3": null, "235d277ff53390255c6c505a1634c88646fa3748": null, "5c06d93f2b2ef968478235270d24c91b90cd857d": null, "0b8350fa9af813124cf6d039ef2c932297fb1b49": null, "547d1d42798933363859853e775d42974080a291": null, "08610b715244a8ddfb3aeb1b4585c4487afd67bd": null, "88ea9aadb49d3b4da1d17d2224e6d4b82a532a40": null, "49eba2416e95c88ac3ec279a1534884429872d21": null, "817f3b1c7ac9220fca3a9275926dabdfb914d885": null, "206f941b5d992f43d5123b2a42c34db758ecc826": null, "60a22e72ff54fa91b04376d6c373f35da19d94ba_currency": null, "1326c4f84ec8611a202cc58a66d091617b910b4d": null, "e13d704375bb3ee3c4523ba2e1e2f79779329615": null, "d9bc80190727512287a4951e160b0f81d8b8f227": null, "7cafce1dd6b89b14f69c23d9f1f56e8f67a8c00b": null, "a451543d82d05cd98464d2c8b4141f95038e7e55": null, "67fcb1f393b0bff6c83d43414cd432b695a4d9c2": null, "3776ab9561a6f50d3becfd63aae885a3f9a6ce86": null, "0f3c7cb8eda95298a7e17cc4c5b6aea1fed99476": null, "b5039fda6e758955f6aefcdf86bc654fd2e7d8bd": null, "5c194672d7994903077944047cec441673e7b1e6": null, "f04a5560d8e408620c633230e141bf16a6874b30": null, "2502aab37117657e5fe04c2ce688b80b8e7aa17f": null, "ef704e3b5bc356ebceb81f00628e5813b8ecf2e5": null, "f4aaa546fb4bc886aed645789ccaaebad4fe60d8": null, "eefb3dbc53aa0d99ef0a57a864af562f6f6de49a": null, "5144ea977b5678f238d5aa11418510c18efcf4fc": null, "stage_order_nr": 5, "person_name": "Person", "org_name": "Test", "next_activity_subject": null, "next_activity_type": null, "next_activity_duration": null, "next_activity_note": null, "formatted_value": "€ 200", "weighted_value": 190, "formatted_weighted_value": "€ 190", "rotten_time": null, "owner_name": "[censored]", "cc_email": "[censored]com2+deal1@pipedrivemail.com", "org_hidden": false, "person_hidden": false, "average_time_to_won": { "y": 0, "m": 0, "d": 0, "h": 0, "i": 0, "s": 0, "total_seconds": 0 }, "average_stage_progress": 0, "age": { "y": 0, "m": 1, "d": 17, "h": 18, "i": 18, "s": 28, "total_seconds": 4126708 }, "stay_in_pipeline_stages": { "times_in_stages": { "1": 435229, "2": 0, "3": 0, "4": 63812, "9": 692, "10": 0, "11": 0, "12": 0, "13": 3626975 }, "order_of_stages": [ 11, 12, 13, 10, 9, 2, 3, 4, 1 ] }, "last_activity": null, "next_activity": null }, "additional_data": { "dropbox_email": "[censored]com2+deal1@pipedrivemail.com" }, "related_objects": { "organization": { "1": { "id": 1, "name": "Test", "people_count": 1, "cc_email": "[censored]com2@pipedrivemail.com" } }, "user": { "[censored]": { "id": [censored], "name": "[censored]", "email": "administratie@[censored]", "has_pic": false, "pic_hash": null } }, "person": { "1": { "id": 1, "name": "Person", "email": "[censored]@[censored]", "phone": "[censored]" } } } }
对此:
class PipeDriveDeals
{
public class UserId
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public bool has_pic { get; set; }
public object pic_hash { get; set; }
public int value { get; set; }
}
public class PersonId
{
public string name { get; set; }
public string email { get; set; }
public object phone { get; set; }
public int value { get; set; }
}
public class OrgId
{
public string name { get; set; }
public int people_count { get; set; }
public string cc_email { get; set; }
public int value { get; set; }
}
public class __invalid_type__0f3c7cb8eda95298a7e17cc4c5b6aea1fed99476
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public bool has_pic { get; set; }
public string pic_hash { get; set; }
public int value { get; set; }
}
public class AverageTimeToWon
{
public int y { get; set; }
public int m { get; set; }
public int d { get; set; }
public int h { get; set; }
public int i { get; set; }
public int s { get; set; }
public int total_seconds { get; set; }
}
public class Age
{
public int y { get; set; }
public int m { get; set; }
public int d { get; set; }
public int h { get; set; }
public int i { get; set; }
public int s { get; set; }
public int total_seconds { get; set; }
}
public class TimesInStages
{
public int pref_11 { get; set; }
public int pref_12 { get; set; }
}
public class StayInPipelineStages
{
public TimesInStages times_in_stages { get; set; }
public List<int> order_of_stages { get; set; }
}
public class data
{
public int id { get; set; }
public int user_id { get; set; }
public int person_id { get; set; }
public int org_id { get; set; }
public int stage_id { get; set; }
public string title { get; set; }
public int value { get; set; }
public string currency { get; set; }
public string add_time { get; set; }
public string update_time { get; set; }
public string stage_change_time { get; set; }
public bool active { get; set; }
public bool deleted { get; set; }
public string status { get; set; }
public DateTime next_activity_date { get; set; }
public DateTime next_activity_time { get; set; }
public int next_activity_id { get; set; }
public int last_activity_id { get; set; }
public DateTime last_activity_date { get; set; }
public string lost_reason { get; set; }
public string visible_to { get; set; }
public DateTime close_time { get; set; }
public int pipeline_id { get; set; }
public DateTime won_time { get; set; }
public DateTime lost_time { get; set; }
public int products_count { get; set; }
public int files_count { get; set; }
public int notes_count { get; set; }
public int followers_count { get; set; }
public int email_messages_count { get; set; }
public int activities_count { get; set; }
public int done_activities_count { get; set; }
public int undone_activities_count { get; set; }
public int reference_activities_count { get; set; }
public int participants_count { get; set; }
public DateTime expected_close_date { get; set; }
[JsonProperty(PropertyName = "f97e4841747d1fc621809ebbd0063f52f4eecf74")]
public string f97e4841747d1fc621809ebbd0063f52f4eecf74 { get; set; }
[JsonProperty(PropertyName = "052d4ce58edf16df90dab070591bd070f8ad88e7")]
public string pref_052d4ce58edf16df90dab070591bd070f8ad88e7 { get; set; }
[JsonProperty(PropertyName = "28fb37b17020f6dbd45ee92c94ae34811e94edd8")]
public string pref_28fb37b17020f6dbd45ee92c94ae34811e94edd8 { get; set; }
[JsonProperty(PropertyName = "a1032d833c7cba5a18ca09cd5418bfa3fd919137_currency")]
public string a1032d833c7cba5a18ca09cd5418bfa3fd919137_currency { get; set; }
[JsonProperty(PropertyName = "d7c2cb45d01c81159dd684111aa41bbbe5ce6db5_currency")]
public string d7c2cb45d01c81159dd684111aa41bbbe5ce6db5_currency { get; set; }
[JsonProperty(PropertyName = "d54e4d7b41a5bdfb2a29f7d57d8c1a4fd2ece115_currency")]
public string d54e4d7b41a5bdfb2a29f7d57d8c1a4fd2ece115_currency { get; set; }
[JsonProperty(PropertyName = "267976bb28be655ecf0bed63efbf4822b75aa0cf_currency")]
public string pref_267976bb28be655ecf0bed63efbf4822b75aa0cf_currency { get; set; }
[JsonProperty(PropertyName = "28e42860f533c6837224736257b963f3d8418ad1")]
public string pref_28e42860f533c6837224736257b963f3d8418ad1 { get; set; }
[JsonProperty(PropertyName = "211a67285816fc5179c248ef08e52000a2d1b15b_currency")]
public string pref_211a67285816fc5179c248ef08e52000a2d1b15b_currency { get; set; }
[JsonProperty(PropertyName = "1697fe1baa42670ccebdb863bc03729b56d8857d_currency")]
public string pref_1697fe1baa42670ccebdb863bc03729b56d8857d_currency { get; set; }
[JsonProperty(PropertyName = "4dc8edc835c39dbe422f3e1dd14f2fa3072866a8_currency")]
public string pref_4dc8edc835c39dbe422f3e1dd14f2fa3072866a8_currency { get; set; }
[JsonProperty(PropertyName = "54dce1f16a984fa7d62e9c31c4c09d8832c0e423_currency")]
public string pref_54dce1f16a984fa7d62e9c31c4c09d8832c0e423_currency { get; set; }
[JsonProperty(PropertyName = "8985469f141b2df5f3236947983feea015d24145")]
public string pref_8985469f141b2df5f3236947983feea015d24145 { get; set; }
[JsonProperty(PropertyName = "05a20487e9aa7a069f678aa2af92be5c75675416")]
public string pref_05a20487e9aa7a069f678aa2af92be5c75675416 { get; set; }
[JsonProperty(PropertyName = "275ac9794b3f274c1c61b47033b298d94a0125fd")]
public string pref_275ac9794b3f274c1c61b47033b298d94a0125fd { get; set; }
[JsonProperty(PropertyName = "24363789868fc8fada2400a3644cb11f7683df04")]
public string pref_24363789868fc8fada2400a3644cb11f7683df04 { get; set; }
[JsonProperty(PropertyName = "0670e6a82e67e19166f11c11778229d783395d0f")]
public string pref_0670e6a82e67e19166f11c11778229d783395d0f { get; set; }
[JsonProperty(PropertyName = "3b0feefce60bc79e9d9fb15bc20c57a05e0ebff8")]
public string pref_3b0feefce60bc79e9d9fb15bc20c57a05e0ebff8 { get; set; }
[JsonProperty(PropertyName = "d3748987f13e9dcdd60ffc6a567d3fd80431d77f")]
public string d3748987f13e9dcdd60ffc6a567d3fd80431d77f { get; set; }
[JsonProperty(PropertyName = "c4fbdf6431406db25949cdd90dfc91eeb5295165")]
public string c4fbdf6431406db25949cdd90dfc91eeb5295165 { get; set; }
[JsonProperty(PropertyName = "c357146d49b62fe08d34bbc98550b2b96431f773")]
public int c357146d49b62fe08d34bbc98550b2b96431f773 { get; set; }
[JsonProperty(PropertyName = "91bd07ed77e652cf6ac4a8b82c83e7e9932dbff3")]
public string pref_91bd07ed77e652cf6ac4a8b82c83e7e9932dbff3 { get; set; }
[JsonProperty(PropertyName = "235d277ff53390255c6c505a1634c88646fa3748")]
public string pref_235d277ff53390255c6c505a1634c88646fa3748 { get; set; }
[JsonProperty(PropertyName = "5c06d93f2b2ef968478235270d24c91b90cd857d")]
public string pref_5c06d93f2b2ef968478235270d24c91b90cd857d { get; set; }
[JsonProperty(PropertyName = "0b8350fa9af813124cf6d039ef2c932297fb1b49")]
public string pref_0b8350fa9af813124cf6d039ef2c932297fb1b49 { get; set; }
[JsonProperty(PropertyName = "547d1d42798933363859853e775d42974080a291")]
public string pref_547d1d42798933363859853e775d42974080a291 { get; set; }
[JsonProperty(PropertyName = "08610b715244a8ddfb3aeb1b4585c4487afd67bd")]
public string pref_08610b715244a8ddfb3aeb1b4585c4487afd67bd { get; set; }
[JsonProperty(PropertyName = "88ea9aadb49d3b4da1d17d2224e6d4b82a532a40")]
public string pref_88ea9aadb49d3b4da1d17d2224e6d4b82a532a40 { get; set; }
[JsonProperty(PropertyName = "49eba2416e95c88ac3ec279a1534884429872d21")]
public string pref_49eba2416e95c88ac3ec279a1534884429872d21 { get; set; }
[JsonProperty(PropertyName = "817f3b1c7ac9220fca3a9275926dabdfb914d885")]
public string pref_817f3b1c7ac9220fca3a9275926dabdfb914d885 { get; set; }
[JsonProperty(PropertyName = "206f941b5d992f43d5123b2a42c34db758ecc826")]
public string pref_206f941b5d992f43d5123b2a42c34db758ecc826 { get; set; }
[JsonProperty(PropertyName = "60a22e72ff54fa91b04376d6c373f35da19d94ba_currency")]
public string pref_60a22e72ff54fa91b04376d6c373f35da19d94ba_currency { get; set; }
[JsonProperty(PropertyName = "1326c4f84ec8611a202cc58a66d091617b910b4d")]
public string pref_1326c4f84ec8611a202cc58a66d091617b910b4d { get; set; }
[JsonProperty(PropertyName = "e13d704375bb3ee3c4523ba2e1e2f79779329615")]
public string e13d704375bb3ee3c4523ba2e1e2f79779329615 { get; set; }
[JsonProperty(PropertyName = "d9bc80190727512287a4951e160b0f81d8b8f227")]
public string d9bc80190727512287a4951e160b0f81d8b8f227 { get; set; }
[JsonProperty(PropertyName = "7cafce1dd6b89b14f69c23d9f1f56e8f67a8c00b")]
public string pref_7cafce1dd6b89b14f69c23d9f1f56e8f67a8c00b { get; set; }
[JsonProperty(PropertyName = "a451543d82d05cd98464d2c8b4141f95038e7e55")]
public string a451543d82d05cd98464d2c8b4141f95038e7e55 { get; set; }
[JsonProperty(PropertyName = "67fcb1f393b0bff6c83d43414cd432b695a4d9c2")]
public string pref_67fcb1f393b0bff6c83d43414cd432b695a4d9c2 { get; set; }
[JsonProperty(PropertyName = "3776ab9561a6f50d3becfd63aae885a3f9a6ce86")]
public string pref_3776ab9561a6f50d3becfd63aae885a3f9a6ce86 { get; set; }
[JsonProperty(PropertyName = "0f3c7cb8eda95298a7e17cc4c5b6aea1fed99476")]
public __invalid_type__0f3c7cb8eda95298a7e17cc4c5b6aea1fed99476 pref_0f3c7cb8eda95298a7e17cc4c5b6aea1fed99476 { get; set; }
[JsonProperty(PropertyName = "b5039fda6e758955f6aefcdf86bc654fd2e7d8bd")]
public string b5039fda6e758955f6aefcdf86bc654fd2e7d8bd { get; set; }
[JsonProperty(PropertyName = "5c194672d7994903077944047cec441673e7b1e6")]
public string pref_5c194672d7994903077944047cec441673e7b1e6 { get; set; }
[JsonProperty(PropertyName = "f04a5560d8e408620c633230e141bf16a6874b30")]
public double f04a5560d8e408620c633230e141bf16a6874b30 { get; set; }
[JsonProperty(PropertyName = "2502aab37117657e5fe04c2ce688b80b8e7aa17f")]
public double pref_2502aab37117657e5fe04c2ce688b80b8e7aa17f { get; set; }
[JsonProperty(PropertyName = "ef704e3b5bc356ebceb81f00628e5813b8ecf2e5")]
public string ef704e3b5bc356ebceb81f00628e5813b8ecf2e5 { get; set; }
[JsonProperty(PropertyName = "f4aaa546fb4bc886aed645789ccaaebad4fe60d8")]
public string f4aaa546fb4bc886aed645789ccaaebad4fe60d8 { get; set; }
[JsonProperty(PropertyName = "5144ea977b5678f238d5aa11418510c18efcf4fc")]
public string pref_5144ea977b5678f238d5aa11418510c18efcf4fc { get; set; }
[JsonProperty(PropertyName = "eefb3dbc53aa0d99ef0a57a864af562f6f6de49a")]
public string pref_eefb3dbc53aa0d99ef0a57a864af562f6f6de49a { get; set; }
public int stage_order_nr { get; set; }
public string person_name { get; set; }
public string org_name { get; set; }
public string next_activity_subject { get; set; }
public string next_activity_type { get; set; }
public string next_activity_duration { get; set; }
public string next_activity_note { get; set; }
public string formatted_value { get; set; }
public int weighted_value { get; set; }
public string formatted_weighted_value { get; set; }
public string rotten_time { get; set; }
public string owner_name { get; set; }
public string cc_email { get; set; }
public bool org_hidden { get; set; }
public bool person_hidden { get; set; }
public AverageTimeToWon average_time_to_won { get; set; }
public int average_stage_progress { get; set; }
public Age age { get; set; }
public StayInPipelineStages stay_in_pipeline_stages { get; set; }
public string last_activity { get; set; }
public string next_activity { get; set; }
}
public class AdditionalData
{
public string dropbox_email { get; set; }
}
public class __invalid_type__509734
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public bool has_pic { get; set; }
public object pic_hash { get; set; }
}
public class User
{
public __invalid_type__509734 pref_509734 { get; set; }
}
public class __invalid_type__22964
{
public int id { get; set; }
public string name { get; set; }
public int people_count { get; set; }
public string cc_email { get; set; }
}
public class Organization
{
public __invalid_type__22964 pref_22964 { get; set; }
}
public class __invalid_type__30607
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public object phone { get; set; }
}
public class Person
{
public __invalid_type__30607 pref_30607 { get; set; }
}
public class RelatedObjects
{
public User user { get; set; }
public Organization organization { get; set; }
public Person person { get; set; }
}
public class RootObject
{
public bool success { get; set; }
public List<data> data { get; set; }
public AdditionalData additional_data { get; set; }
public RelatedObjects related_objects { get; set; }
}
}
- 在不同的 classes 中处理所有内容,以便计算和打印结果。 (此代码与问题无关)
在调试时我发现请求是正确的,返回的数据正是我所期望的(如第三步所示)并且在反序列化期间缓冲区被所述数据填充,但它 不是 用预期值填充 returnvar
class:
我不知道为什么会这样,毕竟我与 PipeDrive API 进行了另外两次交互,它们使用完全相同的方法,具有非常相似的实现,并且工作得很好。
我还注意到 PipeDrive 使用随机字符串作为自定义字段的标识。其中一些往往以数字开头,这意味着不可能创建具有相同名称的变量(因此 pref_
和 __invalid_type__
前缀)。我试图通过设置 JsonProperty
来解决这个问题。这可能是问题所在吗?我的解决方法还不够吗?
我来晚了,但 post 对于那些处理类似问题的人来说,答案似乎是公平的:事实证明这毕竟是一个菜鸟错误。这个错误的答案是我试图填写 data
class 而不是 RootObject
,这在给定层次结构的情况下是行不通的。