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

标签 c# json ssis json-deserialization

我正在尝试反序列化来自 API 的 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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5625373b37243f233b3124392326263a16313b373f3a7835393b" rel="noreferrer noopener nofollow">[email protected]</a>",
            "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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd8e8b988f94928ebd8e94918b988f9e95989bd39e9290d39c88" rel="noreferrer noopener nofollow">[email protected]</a>",
                "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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9fedf0fddff3f0fef1ecf9f0edf7f0f2faecb1fcf0f2b1feea" rel="noreferrer noopener nofollow">[email protected]</a>",
            "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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8a2bba5a9a6a1a7bcbca788afa7afadbcbca9e6aba7a5e6a9bd" rel="noreferrer noopener nofollow">[email protected]</a>",
                "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# 类对象,由 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);”上不断收到“没有为“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\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: 

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

最佳答案

如果您仔细查看您的 json,您会发现返回了两个潜在客户,并且您那里的 JSON 格式不正确。 JSON 必须类似于

     [
{
"id": 1482998,
"campaign_id": 4352,
"salutation": null,
"first_name": "Patrick",
"last_name": "Sam",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c2f3d313d2e3529313b2e33292c2c301c3b313d3530723f3331" rel="noreferrer noopener nofollow">[email protected]</a>",
"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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="44373221362d2b3704372d28322136272c21226a272b296a2531" rel="noreferrer noopener nofollow">[email protected]</a>",
    "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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cebca1ac8ea2a1afa0bda8a1bca6a1a3abbde0ada1a3e0afbb" rel="noreferrer noopener nofollow">[email protected]</a>",
"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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d574e505c5354524949527d5a525a5849495c135e5250135c48" rel="noreferrer noopener nofollow">[email protected]</a>",
    "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);

希望这有帮助

关于带有变量标签的 C# JSON - 没有为 'System.String' 类型定义无参数构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44532064/

相关文章:

python - 使用Bash工具解析JSON

php - 将PHP字符串中两个字符之间的数字相乘

ios - 如何映射 json 响应并创建带索引的简单列表

sql-server - BIML 启用 SSIS 包配置

c# - 在 SSIS 脚本任务中为 C# 中的 for 循环转换字符串

c# - ASP.NET Core 5.0 RouteDataRequestCultureProvider 删除 url 中的默认文化

c# - .Net 中的接口(interface)早期/晚期绑定(bind)

sql-server - SSIS 包 - 在 Excel 连接中保留相同的连接属性

c# - System.Web.Helpers.Json.Deserialize() maxJsonLength 被忽略

c# - 具有复杂板表示的 AI : simulating a single possible move