带有变量标记的 C# JSON - 没有为 'System.String' 的类型定义无参数构造函数

C# JSON with Variable Tags - No parameterless constructor defined for type of 'System.String'

我正在尝试反序列化来自 API 的 JSON 响应,该响应具有变量 JSON 标签,这些标签可能是也可能不是 Prospect 对象的一部分。

这是我用来反序列化的代码。我被限制在 .NET 4.0 中使用 Visual Studio 2010,因为这是更大的 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; }
}

但是我一直在 "prospectWSResponse = wsResponseSerializer.Deserialize(wsResponse);" 行收到 "No parameterless constructor defined for type of 'System.String'" 错误。这是堆栈跟踪。

    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\Vsta7a7a6020bc4142b8255608c2147c78\main.cs:line 146
       at ScriptMain.CreateNewOutputRows() in c:\Users\lp1.db\AppData\Local\Temp\Vsta7a7a6020bc4142b8255608c2147c78\main.cs:line 37
       at UserComponent.PrimeOutput(Int32 Outputs, Int32[] OutputIDs, PipelineBuffer[] Buffers, OutputNameMap OutputMap) in c:\Users\lp1.db\AppData\Local\Temp\Vsta7a7a6020bc4142b8255608c2147c78\ComponentWrapper.cs:line 49
       at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.PrimeOutput(Int32 outputs, Int32[] outputIDs, PipelineBuffer[] buffers)
  InnerException: 

任何有关如何处理这些变量标签(可能包含也可能不包含)的帮助都会非常有帮助。

如果您仔细查看 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);

希望对您有所帮助