【问题标题】:C# JSON with Variable Tags - No parameterless constructor defined for type of 'System.String'带有变量标签的 C# JSON - 没有为“System.String”类型定义无参数构造函数
【发布时间】:2017-06-13 21:43:43
【问题描述】:

我正在尝试反序列化来自具有可变 JSON 标记的 API 的 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
          }]

这是我由 json2csharp 生成的 C# 类对象

    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: 

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

【问题讨论】:

    标签: c# json ssis json-deserialization


    【解决方案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);
    

    希望对你有帮助

    【讨论】:

    • 这只是 JSON 的摘录,以显示潜在客户之间的字段不同。由于前端的用户定义字段,前景中包含一组核心标签和一组可变标签。
    • 我已更改此设置并收到此错误。 无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“ScriptMain.RootObject”。存在显式转换(您是否缺少强制转换?) 我需要更改我的类定义。
    • 你能用 var 前景WSResponse = wsResponseSerializer.Deserialize>(wsResponse); ?
    • 我收到这个错误是因为我已经在这里启动了前景WSResponse RootObject prospectWSResponse = null; 一个名为“prospectWSResponse”的局部变量已经在这个范围内定义了。如果我更改名称,我仍然会收到上述错误。
    • 好的。所以不要启动前景WSResponse = null 。删除启动它的行,并在您实际反序列化它的地方,写下这一行, var前景WSResponse = wsResponseSerializer.Deserialize>(ws‌​Response);
    猜你喜欢
    • 2020-08-31
    • 2012-03-12
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    相关资源
    最近更新 更多