System.Reflection.AmbiguousMatchException 正在解析 json
System.Reflection.AmbiguousMatchException while parsing json
不幸的是,我对 C# 的经验为零,但我需要在 SSIS 中创建 JSON 源代码。
我正在尝试下载 JSON、反序列化并将其保存到列表:
public override void CreateNewOutputRows()
{
JavaScriptSerializer serialize = new JavaScriptSerializer();
var employees = new List<Employees>();
employees = serialize.Deserialize<List<Employees>>(json);
foreach (var record in employees)
{
Output0Buffer.AddRow();
Output0Buffer.FirstName = record.FirstName;
Output0Buffer.EmailId = record.EmailID;
Output0Buffer.Profile = record.Profile;
Output0Buffer.EmployeeIDInternal = record.EmployeeIDInternal;
Output0Buffer.LastName = record.LastName;
}
}
员工是:
public class Employees
{
public string FirstName { get; set; }
public string EmailID { get; set; }
public string Profile { get; set; }
public string EmployeeIDInternal { get; set; }
public string LastName { get; set; }
}
但失败并出现以下错误:
System.Reflection.AmbiguousMatchException
HResult=0x8000211D
Message=Ambiguous match found.
Source=mscorlib
更新:
@OlivierJacot-Descombes 我无法向您展示原始 json,但我通过从序列化器创建 json 尝试了 JavaScriptSerializer 方法文档中的方法:
var RegisteredUsers = new List<Employees>();
RegisteredUsers.Add(new Employees() { FirstName = "Name1", EmailID = "abc@gmail.com", Profile = "DB", EmployeeIDInternal = "123", LastName = "Surname1" });
RegisteredUsers.Add(new Employees() { FirstName = "Name2", EmailID = "abc1@gmail.com", Profile = "DB1", EmployeeIDInternal = "1234", LastName = "Surname2" });
RegisteredUsers.Add(new Employees() { FirstName = "Name3", EmailID = "abc234@gmail.com", Profile = "SQL", EmployeeIDInternal = "77", LastName = "Surname3" });
RegisteredUsers.Add(new Employees() { FirstName = "Name4", EmailID = "absdfsc@gmail.com", Profile = "", EmployeeIDInternal = "555", LastName = "Surname4" });
var serializer = new JavaScriptSerializer();
var serializedResult = serializer.Serialize(RegisteredUsers);
var employees = serializer.Deserialize<List<Employees>>(serializedResult);
它生成以下 json 文本(原始文本在一行文本上未格式化):
[
{
"modifiedTime":null,
"Addedtime":null,
"FirstName":"Name1",
"EmailID":"abc@gmail.com",
"Photo":null,
"HRSpecialist":null,
"AddedBy":null,
"Gender":null,
"ApprovalStatus":null,
"Profile":"DB",
"CarNumberPlate":null,
"recordId":null,
"ModifiedBy":null,
"Department":null,
"EmployeeIDInternal":"123",
"Modifiedtime":null,
"ownerName":null,
"createdTime":null,
"TechnicalLevel":null,
"Room":null,
"Tags":null,
"Photo_downloadUrl":null,
"ReportingTo":null,
"FirstProject":null,
"Designation":null,
"IM":null,
"WorkLocation":null,
"ownerID":null,
"MaritalStatus":null,
"DateofJoining":null,
"IMServiceProvider":null,
"CompetenceLead":null,
"DateofBirth":null,
"LastName":"Surname1",
"EmployeeID":null,
"MobilePhone":null,
"Expertise":null,
"Location":null
},
{
"modifiedTime":null,
"Addedtime":null,
"FirstName":"Name2",
"EmailID":"abc1@gmail.com",
"Photo":null,
"HRSpecialist":null,
"AddedBy":null,
"Gender":null,
"ApprovalStatus":null,
"Profile":"DB1",
"CarNumberPlate":null,
"recordId":null,
"ModifiedBy":null,
"Department":null,
"EmployeeIDInternal":"1234",
"Modifiedtime":null,
"ownerName":null,
"createdTime":null,
"TechnicalLevel":null,
"Room":null,
"Tags":null,
"Photo_downloadUrl":null,
"ReportingTo":null,
"FirstProject":null,
"Designation":null,
"IM":null,
"WorkLocation":null,
"ownerID":null,
"MaritalStatus":null,
"DateofJoining":null,
"IMServiceProvider":null,
"CompetenceLead":null,
"DateofBirth":null,
"LastName":"Surname2",
"EmployeeID":null,
"MobilePhone":null,
"Expertise":null,
"Location":null
},
{
"modifiedTime":null,
"Addedtime":null,
"FirstName":"Name3",
"EmailID":"abc234@gmail.com",
"Photo":null,
"HRSpecialist":null,
"AddedBy":null,
"Gender":null,
"ApprovalStatus":null,
"Profile":"SQL",
"CarNumberPlate":null,
"recordId":null,
"ModifiedBy":null,
"Department":null,
"EmployeeIDInternal":"77",
"Modifiedtime":null,
"ownerName":null,
"createdTime":null,
"TechnicalLevel":null,
"Room":null,
"Tags":null,
"Photo_downloadUrl":null,
"ReportingTo":null,
"FirstProject":null,
"Designation":null,
"IM":null,
"WorkLocation":null,
"ownerID":null,
"MaritalStatus":null,
"DateofJoining":null,
"IMServiceProvider":null,
"CompetenceLead":null,
"DateofBirth":null,
"LastName":"Surname3",
"EmployeeID":null,
"MobilePhone":null,
"Expertise":null,
"Location":null
},
{
"modifiedTime":null,
"Addedtime":null,
"FirstName":"Name4",
"EmailID":"absdfsc@gmail.com",
"Photo":null,
"HRSpecialist":null,
"AddedBy":null,
"Gender":null,
"ApprovalStatus":null,
"Profile":"",
"CarNumberPlate":null,
"recordId":null,
"ModifiedBy":null,
"Department":null,
"EmployeeIDInternal":"555",
"Modifiedtime":null,
"ownerName":null,
"createdTime":null,
"TechnicalLevel":null,
"Room":null,
"Tags":null,
"Photo_downloadUrl":null,
"ReportingTo":null,
"FirstProject":null,
"Designation":null,
"IM":null,
"WorkLocation":null,
"ownerID":null,
"MaritalStatus":null,
"DateofJoining":null,
"IMServiceProvider":null,
"CompetenceLead":null,
"DateofBirth":null,
"LastName":"Surname4",
"EmployeeID":null,
"MobilePhone":null,
"Expertise":null,
"Location":null
}
]
Employees
class 的属性或字段似乎比您向我们展示的要多得多。
在您的 JSON 文本中,我看到每条记录有两个相似的条目:
"modifiedTime":null, field #1
"Modifiedtime":null, field #16
它们只是通过字母大小写来区分的。它们很可能是 System.Reflection.AmbiguousMatchException
的起源。重命名其中之一或删除其中之一。
一个可能是 属性 而另一个是字段。参见:How to exclude property from Json Serialization
不幸的是,我对 C# 的经验为零,但我需要在 SSIS 中创建 JSON 源代码。
我正在尝试下载 JSON、反序列化并将其保存到列表:
public override void CreateNewOutputRows()
{
JavaScriptSerializer serialize = new JavaScriptSerializer();
var employees = new List<Employees>();
employees = serialize.Deserialize<List<Employees>>(json);
foreach (var record in employees)
{
Output0Buffer.AddRow();
Output0Buffer.FirstName = record.FirstName;
Output0Buffer.EmailId = record.EmailID;
Output0Buffer.Profile = record.Profile;
Output0Buffer.EmployeeIDInternal = record.EmployeeIDInternal;
Output0Buffer.LastName = record.LastName;
}
}
员工是:
public class Employees
{
public string FirstName { get; set; }
public string EmailID { get; set; }
public string Profile { get; set; }
public string EmployeeIDInternal { get; set; }
public string LastName { get; set; }
}
但失败并出现以下错误:
System.Reflection.AmbiguousMatchException
HResult=0x8000211D
Message=Ambiguous match found.
Source=mscorlib
更新: @OlivierJacot-Descombes 我无法向您展示原始 json,但我通过从序列化器创建 json 尝试了 JavaScriptSerializer 方法文档中的方法:
var RegisteredUsers = new List<Employees>();
RegisteredUsers.Add(new Employees() { FirstName = "Name1", EmailID = "abc@gmail.com", Profile = "DB", EmployeeIDInternal = "123", LastName = "Surname1" });
RegisteredUsers.Add(new Employees() { FirstName = "Name2", EmailID = "abc1@gmail.com", Profile = "DB1", EmployeeIDInternal = "1234", LastName = "Surname2" });
RegisteredUsers.Add(new Employees() { FirstName = "Name3", EmailID = "abc234@gmail.com", Profile = "SQL", EmployeeIDInternal = "77", LastName = "Surname3" });
RegisteredUsers.Add(new Employees() { FirstName = "Name4", EmailID = "absdfsc@gmail.com", Profile = "", EmployeeIDInternal = "555", LastName = "Surname4" });
var serializer = new JavaScriptSerializer();
var serializedResult = serializer.Serialize(RegisteredUsers);
var employees = serializer.Deserialize<List<Employees>>(serializedResult);
它生成以下 json 文本(原始文本在一行文本上未格式化):
[
{
"modifiedTime":null,
"Addedtime":null,
"FirstName":"Name1",
"EmailID":"abc@gmail.com",
"Photo":null,
"HRSpecialist":null,
"AddedBy":null,
"Gender":null,
"ApprovalStatus":null,
"Profile":"DB",
"CarNumberPlate":null,
"recordId":null,
"ModifiedBy":null,
"Department":null,
"EmployeeIDInternal":"123",
"Modifiedtime":null,
"ownerName":null,
"createdTime":null,
"TechnicalLevel":null,
"Room":null,
"Tags":null,
"Photo_downloadUrl":null,
"ReportingTo":null,
"FirstProject":null,
"Designation":null,
"IM":null,
"WorkLocation":null,
"ownerID":null,
"MaritalStatus":null,
"DateofJoining":null,
"IMServiceProvider":null,
"CompetenceLead":null,
"DateofBirth":null,
"LastName":"Surname1",
"EmployeeID":null,
"MobilePhone":null,
"Expertise":null,
"Location":null
},
{
"modifiedTime":null,
"Addedtime":null,
"FirstName":"Name2",
"EmailID":"abc1@gmail.com",
"Photo":null,
"HRSpecialist":null,
"AddedBy":null,
"Gender":null,
"ApprovalStatus":null,
"Profile":"DB1",
"CarNumberPlate":null,
"recordId":null,
"ModifiedBy":null,
"Department":null,
"EmployeeIDInternal":"1234",
"Modifiedtime":null,
"ownerName":null,
"createdTime":null,
"TechnicalLevel":null,
"Room":null,
"Tags":null,
"Photo_downloadUrl":null,
"ReportingTo":null,
"FirstProject":null,
"Designation":null,
"IM":null,
"WorkLocation":null,
"ownerID":null,
"MaritalStatus":null,
"DateofJoining":null,
"IMServiceProvider":null,
"CompetenceLead":null,
"DateofBirth":null,
"LastName":"Surname2",
"EmployeeID":null,
"MobilePhone":null,
"Expertise":null,
"Location":null
},
{
"modifiedTime":null,
"Addedtime":null,
"FirstName":"Name3",
"EmailID":"abc234@gmail.com",
"Photo":null,
"HRSpecialist":null,
"AddedBy":null,
"Gender":null,
"ApprovalStatus":null,
"Profile":"SQL",
"CarNumberPlate":null,
"recordId":null,
"ModifiedBy":null,
"Department":null,
"EmployeeIDInternal":"77",
"Modifiedtime":null,
"ownerName":null,
"createdTime":null,
"TechnicalLevel":null,
"Room":null,
"Tags":null,
"Photo_downloadUrl":null,
"ReportingTo":null,
"FirstProject":null,
"Designation":null,
"IM":null,
"WorkLocation":null,
"ownerID":null,
"MaritalStatus":null,
"DateofJoining":null,
"IMServiceProvider":null,
"CompetenceLead":null,
"DateofBirth":null,
"LastName":"Surname3",
"EmployeeID":null,
"MobilePhone":null,
"Expertise":null,
"Location":null
},
{
"modifiedTime":null,
"Addedtime":null,
"FirstName":"Name4",
"EmailID":"absdfsc@gmail.com",
"Photo":null,
"HRSpecialist":null,
"AddedBy":null,
"Gender":null,
"ApprovalStatus":null,
"Profile":"",
"CarNumberPlate":null,
"recordId":null,
"ModifiedBy":null,
"Department":null,
"EmployeeIDInternal":"555",
"Modifiedtime":null,
"ownerName":null,
"createdTime":null,
"TechnicalLevel":null,
"Room":null,
"Tags":null,
"Photo_downloadUrl":null,
"ReportingTo":null,
"FirstProject":null,
"Designation":null,
"IM":null,
"WorkLocation":null,
"ownerID":null,
"MaritalStatus":null,
"DateofJoining":null,
"IMServiceProvider":null,
"CompetenceLead":null,
"DateofBirth":null,
"LastName":"Surname4",
"EmployeeID":null,
"MobilePhone":null,
"Expertise":null,
"Location":null
}
]
Employees
class 的属性或字段似乎比您向我们展示的要多得多。
在您的 JSON 文本中,我看到每条记录有两个相似的条目:
"modifiedTime":null, field #1
"Modifiedtime":null, field #16
它们只是通过字母大小写来区分的。它们很可能是 System.Reflection.AmbiguousMatchException
的起源。重命名其中之一或删除其中之一。
一个可能是 属性 而另一个是字段。参见:How to exclude property from Json Serialization