我正在尝试从具有可变JSON标记的API反序列化JSON响应,该标记可能是也可能不是Prospect对象的一部分。
这是我用来反序列化的代码。我被限制使用Visual Studio 2010和.NET 4.0,因为这是更大的SSIS包的一部分。
//Capture Prospect Web Service POST API Response
HttpWebResponse prospectResponse = (HttpWebResponse)prospectReq.GetResponse();
RootObject prospectWSResponse = null;
Stream prospectWSResponseStream = prospectResponse.GetResponseStream();
string wsResponse = null;
using (StreamReader wsProspectReader = new StreamReader(prospectWSResponseStream))
{
wsResponse = wsProspectReader.ReadToEnd();
System.Windows.Forms.MessageBox.Show(wsResponse);
wsProspectReader.Close();
}
JavaScriptSerializer wsResponseSerializer = new JavaScriptSerializer();
prospectWSResponse = wsResponseSerializer.Deserialize<RootObject>(wsResponse);
return prospectWSResponse;
}
这是响应的摘录,因此您可以通过变量标签获得我的意思。标签可能包含也可能不包含,因为它们是UI中的自定义字段。
"prospect": [
{
"id": 1482998,
"campaign_id": 4352,
"salutation": null,
"first_name": "Patrick",
"last_name": "Sam",
"email": "samariumgrouppl@gmail.com",
"password": null,
"company": "Kenko Home Cuisine",
"website": null,
"job_title": null,
"department": null,
"country": "Australia",
"address_one": null,
"address_two": null,
"city": null,
"state": "Western Australia",
"territory": null,
"zip": null,
"phone": "0430058000",
"fax": null,
"source": "Web (Marketing)",
"annual_revenue": 0,
"employees": null,
"industry": null,
"years_in_business": null,
"comments": null,
"notes": null,
"score": 0,
"grade": null,
"last_activity_at": null,
"recent_interaction": "Never active",
"crm_lead_fid": "00Q6F00000zG7nHUAS",
"crm_contact_fid": null,
"crm_owner_fid": "0056F000006NLEUQA4",
"crm_account_fid": null,
"salesforce_fid": "00Q6F00000zG7nHUAS",
"crm_last_sync": "2017-01-03 11:37:41",
"crm_url": "https://silverchef.my.salesforce.com/00Q6F00000zG7nHUAS",
"is_do_not_email": null,
"is_do_not_call": null,
"opted_out": null,
"is_reviewed": 1,
"is_starred": null,
"created_at": "2016-07-28 13:07:02",
"updated_at": "2017-01-03 11:37:52",
"campaign": {
"id": 4352,
"name": "SC FY17 Q2 contact us"
},
"assigned_to": {
"user": {
"id": 5425592,
"email": "sverios@silverchef.com.au",
"first_name": "Seon",
"last_name": "Verios",
"job_title": null,
"role": "Sales",
"account": 212762,
"created_at": "2016-08-02 11:34:36",
"updated_at": "2016-10-24 15:08:19"
}
},
"Brand": "Silver Chef",
"Enquiry_Details": "Need dealer to supply Coffee Machine & Blender",
"Finance_Amount": 0,
"Lead_Category": "Contact Us",
"Lead_Status": "Closed - Not Converted",
"Nature_of_Business___Silver_Chef": "Restaurant Licensed",
"Page_Source": "https://www.silverchef.com.au/contact-us"
},
{
"id": 1483006,
"campaign_id": 750,
"salutation": "Mr.",
"first_name": "Rob",
"last_name": "Harrap",
"email": "rob@loansforhomes.com.au",
"password": null,
"company": "Loans For Homes",
"website": null,
"job_title": "Director",
"department": null,
"country": "Australia",
"address_one": "PO Box 2067",
"address_two": null,
"city": "Nerang Business Centre",
"state": "Queensland",
"territory": null,
"zip": 4211,
"phone": "0755961499",
"fax": "0755782986",
"source": null,
"annual_revenue": null,
"employees": null,
"industry": null,
"years_in_business": null,
"comments": null,
"notes": null,
"score": 0,
"grade": null,
"last_activity_at": null,
"recent_interaction": "Never active",
"crm_lead_fid": null,
"crm_contact_fid": "0036F00001xUCMPQA4",
"crm_owner_fid": "00590000004cYMTAA2",
"crm_account_fid": null,
"salesforce_fid": "0036F00001xUCMPQA4",
"crm_last_sync": "2016-08-17 19:52:17",
"crm_url": "https://silverchef.my.salesforce.com/0036F00001xUCMPQA4",
"is_do_not_email": null,
"is_do_not_call": null,
"opted_out": null,
"is_reviewed": 1,
"is_starred": null,
"created_at": "2016-07-28 13:09:30",
"updated_at": "2017-05-04 22:51:53",
"campaign": {
"id": 750,
"name": "Salesforce"
},
"assigned_to": {
"user": {
"id": 5425282,
"email": "jsmaniotto@gogetta.com.au",
"first_name": "Jess",
"last_name": "Smaniotto",
"job_title": null,
"role": "Sales",
"account": 212762,
"created_at": "2016-08-02 11:34:27",
"updated_at": "2016-10-24 15:10:44"
}
},
"Authorised_Person": false,
"Broker": true,
"Dealer_Principal": false,
"Development___Shopfitter": false,
"Director": true,
"Director___Owner": false,
"Employee": false,
"Franchise": false,
"mobile": "0412 322 690",
"Remarketing": false,
"Sales_Rep": false,
"prospect_account_id": 28878
}]
这是我的C#Class对象,由json2csharp
生成 public class Prospect
{
public int id { get; set; }
public int campaign_id { get; set; }
public string salutation { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public string password { get; set; }
public string company { get; set; }
public string website { get; set; }
public string job_title { get; set; }
public string department { get; set; }
public string country { get; set; }
public string address_one { get; set; }
public string address_two { get; set; }
public string city { get; set; }
public string state { get; set; }
public string territory { get; set; }
public string zip { get; set; }
public string phone { get; set; }
public string fax { get; set; }
public string source { get; set; }
public double? annual_revenue { get; set; }
public int? employees { get; set; }
public string industry { get; set; }
public int? years_in_business { get; set; }
public string comments { get; set; }
public string notes { get; set; }
public int? score { get; set; }
public string grade { get; set; }
public string last_activity_at { get; set; }
public string recent_interaction { get; set; }
public string crm_lead_fid { get; set; }
public string crm_contact_fid { get; set; }
public string crm_owner_fid { get; set; }
public string crm_account_fid { get; set; }
public string salesforce_fid { get; set; }
public string crm_last_sync { get; set; }
public string crm_url { get; set; }
public int? is_do_not_email { get; set; }
public int? is_do_not_call { get; set; }
public int? opted_out { get; set; }
public int? is_reviewed { get; set; }
public int? is_starred { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public Campaign campaign { get; set; }
public AssignedTo assigned_to { get; set; }
public string Brand { get; set; }
public string Lead_Category { get; set; }
public string Lead_Status { get; set; }
public string Lead_Sub_Category { get; set; }
public string Page_Source { get; set; }
public string Enquiry_Details { get; set; }
public double? Finance_Amount { get; set; }
public string Nature_of_Business___Silver_Chef { get; set; }
public bool? Authorised_Person { get; set; }
public bool? Broker { get; set; }
public bool? Dealer_Principal { get; set; }
public bool? Development___Shopfitter { get; set; }
public bool? Director { get; set; }
public bool? Director___Owner { get; set; }
public bool? Employee { get; set; }
public bool? Franchise { get; set; }
public string mobile { get; set; }
public bool? Remarketing { get; set; }
public bool? Sales_Rep { get; set; }
public int? prospect_account_id { get; set; }
public LastActivity last_activity { get; set; }
public string Dealer_Name { get; set; }
public string Nature_of_Enquiry { get; set; }
public string how_did_you_find_us { get; set; }
public string Equipment_of_Interest { get; set; }
public int? Custom_100_score { get; set; }
public string Promo_Code { get; set; }
public string Asset_Category { get; set; }
public string Asset_Link { get; set; }
public string Asset_Number { get; set; }
public string Asset_Name { get; set; }
public string Opportunity_Name { get; set; }
public string ABN { get; set; }
public string Date_of_Birth { get; set; }
public string Dealer_Email { get; set; }
public string Driver_s_Licence { get; set; }
public string Middle_Name { get; set; }
public int? Additional_Approved_Finance { get; set; }
public string How_long_before_you_plan_to_open_ { get; set; }
public int? Master_Agreement_Finance { get; set; }
public string Dealer_Rep { get; set; }
public string FCM_ID { get; set; }
public string Recipient_Email { get; set; }
public string Recipient_First_Name { get; set; }
public string Recipient_Last_Name { get; set; }
public string review_comments { get; set; }
public string Asset_Sub_Category { get; set; }
public string Broker_Name { get; set; }
public string Broker_State { get; set; }
public string Best_Description_of_You_ { get; set; }
public string Lost_Sale_Quote_Cash_Price { get; set; }
public string Lost_Sale_Quote_Number { get; set; }
}
但是我一直在为#S; System.String&#39;&#34;的类型定义一个&#34; No无参数构造函数。 &#34; prospectWSResponse = wsResponseSerializer.Deserialize(wsResponse);&#34;线。这是堆栈跟踪。
System.MissingMethodException was unhandled by user code
HResult=-2146233069
Message=No parameterless constructor defined for type of 'System.String'.
Source=System.Web.Extensions
StackTrace:
at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.AssignToPropertyOrField(Object propertyValue, Object o, String memberName, JavaScriptSerializer serializer, Boolean throwOnError)
at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.AddItemToList(IList oldList, IList newList, Type elementType, JavaScriptSerializer serializer, Boolean throwOnError)
at System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList list, Type type, JavaScriptSerializer serializer, Boolean throwOnError, IList& convertedList)
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.AssignToPropertyOrField(Object propertyValue, Object o, String memberName, JavaScriptSerializer serializer, Boolean throwOnError)
at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.AssignToPropertyOrField(Object propertyValue, Object o, String memberName, JavaScriptSerializer serializer, Boolean throwOnError)
at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
at ScriptMain.GetProspectWebServiceResult(String prospectapiurl) in c:\Users\lp1.db\AppData\Local\Temp\6\Vsta\387a7a6020bc4142b8255608c2147c78\main.cs:line 146
at ScriptMain.CreateNewOutputRows() in c:\Users\lp1.db\AppData\Local\Temp\6\Vsta\387a7a6020bc4142b8255608c2147c78\main.cs:line 37
at UserComponent.PrimeOutput(Int32 Outputs, Int32[] OutputIDs, PipelineBuffer[] Buffers, OutputNameMap OutputMap) in c:\Users\lp1.db\AppData\Local\Temp\6\Vsta\387a7a6020bc4142b8255608c2147c78\ComponentWrapper.cs:line 49
at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.PrimeOutput(Int32 outputs, Int32[] outputIDs, PipelineBuffer[] buffers)
InnerException:
如何处理这些变量标签(可能包括也可能不包括)的任何帮助都会非常有用。
答案 0 :(得分:1)
如果仔细观察你的json,你会有两个潜在客户返回,你在那里的JSON格式不正确。 JSON必须类似于
[
{
"id": 1482998,
"campaign_id": 4352,
"salutation": null,
"first_name": "Patrick",
"last_name": "Sam",
"email": "samariumgrouppl@gmail.com",
"password": null,
"company": "Kenko Home Cuisine",
"website": null,
"job_title": null,
"department": null,
"country": "Australia",
"address_one": null,
"address_two": null,
"city": null,
"state": "Western Australia",
"territory": null,
"zip": null,
"phone": "0430058000",
"fax": null,
"source": "Web (Marketing)",
"annual_revenue": 0,
"employees": null,
"industry": null,
"years_in_business": null,
"comments": null,
"notes": null,
"score": 0,
"grade": null,
"last_activity_at": null,
"recent_interaction": "Never active",
"crm_lead_fid": "00Q6F00000zG7nHUAS",
"crm_contact_fid": null,
"crm_owner_fid": "0056F000006NLEUQA4",
"crm_account_fid": null,
"salesforce_fid": "00Q6F00000zG7nHUAS",
"crm_last_sync": "2017-01-03 11:37:41",
"crm_url": "https://silverchef.my.salesforce.com/00Q6F00000zG7nHUAS",
"is_do_not_email": null,
"is_do_not_call": null,
"opted_out": null,
"is_reviewed": 1,
"is_starred": null,
"created_at": "2016-07-28 13:07:02",
"updated_at": "2017-01-03 11:37:52",
"campaign": {
"id": 4352,
"name": "SC FY17 Q2 contact us"
},
"assigned_to": {
"user": {
"id": 5425592,
"email": "sverios@silverchef.com.au",
"first_name": "Seon",
"last_name": "Verios",
"job_title": null,
"role": "Sales",
"account": 212762,
"created_at": "2016-08-02 11:34:36",
"updated_at": "2016-10-24 15:08:19"
}
},
"Brand": "Silver Chef",
"Enquiry_Details": "Need dealer to supply Coffee Machine & Blender",
"Finance_Amount": 0,
"Lead_Category": "Contact Us",
"Lead_Status": "Closed - Not Converted",
"Nature_of_Business___Silver_Chef": "Restaurant Licensed",
"Page_Source": "https://www.silverchef.com.au/contact-us"
},
{
"id": 1483006,
"campaign_id": 750,
"salutation": "Mr.",
"first_name": "Rob",
"last_name": "Harrap",
"email": "rob@loansforhomes.com.au",
"password": null,
"company": "Loans For Homes",
"website": null,
"job_title": "Director",
"department": null,
"country": "Australia",
"address_one": "PO Box 2067",
"address_two": null,
"city": "Nerang Business Centre",
"state": "Queensland",
"territory": null,
"zip": 4211,
"phone": "0755961499",
"fax": "0755782986",
"source": null,
"annual_revenue": null,
"employees": null,
"industry": null,
"years_in_business": null,
"comments": null,
"notes": null,
"score": 0,
"grade": null,
"last_activity_at": null,
"recent_interaction": "Never active",
"crm_lead_fid": null,
"crm_contact_fid": "0036F00001xUCMPQA4",
"crm_owner_fid": "00590000004cYMTAA2",
"crm_account_fid": null,
"salesforce_fid": "0036F00001xUCMPQA4",
"crm_last_sync": "2016-08-17 19:52:17",
"crm_url": "httpssilverchef.my.salesforce.com/0036F00001xUCMPQA4",
"is_do_not_email": null,
"is_do_not_call": null,
"opted_out": null,
"is_reviewed": 1,
"is_starred": null,
"created_at": "2016-07-28 13:09:30",
"updated_at": "2017-05-04 22:51:53",
"campaign": {
"id": 750,
"name": "Salesforce"
},
"assigned_to": {
"user": {
"id": 5425282,
"email": "jsmaniotto@gogetta.com.au",
"first_name": "Jess",
"last_name": "Smaniotto",
"job_title": null,
"role": "Sales",
"account": 212762,
"created_at": "2016-08-02 11:34:27",
"updated_at": "2016-10-24 15:10:44"
}
},
"Authorised_Person": false,
"Broker": true,
"Dealer_Principal": false,
"Development___Shopfitter": false,
"Director": true,
"Director___Owner": false,
"Employee": false,
"Franchise": false,
"mobile": "0412 322 690",
"Remarketing": false,
"Sales_Rep": false,
"prospect_account_id": 28878
}
]
表示需要反序列化的数组或项列表。获得正确的JSON后,尝试使用
反序列化它 prospectWSResponse = wsResponseSerializer.Deserialize<IEnumerable<RootObject>>(wsResponse);
希望这有帮助