C# 无法将当前JSON数组(例如[1,2,3])反序列化为具有复杂嵌套对象的类型
我正在尝试为复杂对象构建web api。为此,我构建了自定义绑定器,从JSON反序列化这个对象 我的客户活页夹看起来像:C# 无法将当前JSON数组(例如[1,2,3])反序列化为具有复杂嵌套对象的类型,c#,asp.net-web-api,json.net,jsonserializer,C#,Asp.net Web Api,Json.net,Jsonserializer,我正在尝试为复杂对象构建web api。为此,我构建了自定义绑定器,从JSON反序列化这个对象 我的客户活页夹看起来像: var paramType = p_BindingContext.ModelType; dynamic updatedParam = Activator.CreateInstance(paramType); JsonReader reader = new JTokenReader(JObject.Parse (p_ActionContext.Req
var paramType = p_BindingContext.ModelType;
dynamic updatedParam = Activator.CreateInstance(paramType);
JsonReader reader = new JTokenReader(JObject.Parse
(p_ActionContext.Request.Content.ReadAsStringAsync().Result));
JObject jObject = JObject.Load(reader);
JsonSerializer serializer = new JsonSerializer();
serializer.Populate(jObject.CreateReader(), updatedParam);//Here the exception thorws
p_BindingContext.Model = updatedParam;
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type WebAPI.Models.Entities.Campaigns.CampaignObjects.ProposalItem' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
要序列化的对象非常复杂:
public class ModelRegisterUserRequest{
public ModelUser User { get; set; }
public CampaignItem CampaignWithChosenProposal { get; set; }
public string ValidationToken { get; set; }
public string IVRToken { get; set; }
public string DealerID { get; set; }
public string SalePersonID { get; set; }
}
public class CampaignItem
{
public List<BannerItem> Banners { get; set; }
public string Code { get; set; }
public string CustomerInstruction { get; set; }
public string InfoLink { get; set; }
public string LobbySubTitle { get; set; }
public string LobbyTitle { get; set; }
public string MoreText { get; set; }
public uint NumOfColumns { get; set; }
public uint NumOfRows { get; set; }
public string OriginString { get; set; }
public int OriginInt { get; set; }
public List<ProposalItem> Proposals { get; set; }
public string RegulationsInfoLink { get; set; }
public string ServiceType { get; set; }
public string SubTitle { get; set; }
public string SWDefault { get; set; }
public string Title { get; set; }
}
public partial class ProposalItem
{
public List<string> EquipmentsCode { get; set; }
public string FeatureCode { get; set; }
public string InvalidReason { get; set; }
public bool IsExistsMoreInfo { get; set; }
public bool IsValid { get; set; }
public string LeadCode { get; set; }
public int ParamCombinationCode { get; set; }
public string ProductCode { get; set; }
public string ProductCombinationCode { get; set; }
public string ProposalCode { get; set; }
public List<ColumnItem> Columns { get; set; }
public List<MoreInfoItem> MoreInfoList { get; set; }
public List<string> PricePlans { get; set; }
public string ProductName { get; set; }
公共类ModelRegisterUserRequest{
公共模型用户{get;set;}
公共活动项目CampaignWithChosenProposal{get;set;}
公共字符串验证令牌{get;set;}
公共字符串IVRToken{get;set;}
公共字符串DealerID{get;set;}
公共字符串SalePersonID{get;set;}
}
公开课活动项目
{
公共列表横幅{get;set;}
公共字符串代码{get;set;}
公共字符串CustomerInstruction{get;set;}
公共字符串信息链接{get;set;}
公共字符串LobbySubTitle{get;set;}
公共字符串LobbyTitle{get;set;}
公共字符串MoreText{get;set;}
公共uint NUMOF列{get;set;}
公共uint numorrows{get;set;}
公共字符串OriginString{get;set;}
public int OriginInt{get;set;}
公开列表建议{get;set;}
公共字符串规则信息链接{get;set;}
公共字符串服务类型{get;set;}
公共字符串字幕{get;set;}
公共字符串SWDefault{get;set;}
公共字符串标题{get;set;}
}
公共部分类命题
{
公共列表设备代码{get;set;}
公共字符串特征代码{get;set;}
公共字符串InvalidReason{get;set;}
公共bool IsExistsMoreInfo{get;set;}
公共bool有效{get;set;}
公共字符串引导代码{get;set;}
公共int参数组合代码{get;set;}
公共字符串ProductCode{get;set;}
公共字符串ProductCombinationCode{get;set;}
公共字符串提议码{get;set;}
公共列表列{get;set;}
公共列表MoreInfoList{get;set;}
公共列表价格计划{get;set;}
公共字符串ProductName{get;set;}
}
Json是使用Javascript代码中的Json.stringify命令创建的,如下所示:
{
"ValidationToken": "1cc6cca8-44d5-4042-af37-de6a0d198d17",
"AppID": "TST",
"campaignWithChosenProposal": {
"Banners": [
{
"LocationCodeString": "ManagerTab",
"LocationCodeInt": 256,
"MediaUrl": "<a href=\"c:\\\">BANNER 10</a>"
}
],
"Code": "CAMP221",
"CustomerInstruction": "-1",
"InfoLink": "http://test.aspx",
"LobbySubTitle": "",
"LobbyTitle": "",
"MoreText": "",
"NumOfColumns": 0,
"NumOfRows": 0,
"OriginString": "INT",
"OriginInt": 4,
"Proposals": [
[
{
"EquipmentsCode": [
"5455"
],
"FeatureCode": "BE5455",
"InvalidReason": "",
"IsExistsMoreInfo": true,
"IsValid": true,
"LeadCode": "3792956510",
"ParamCombinationCode": 0,
"ProductCode": "OANTIVRP2",
"ProductCombinationCode": "0",
"ProposalCode": "291600010201C8F83661D5B82FD5F3603967588B7A72",
"Columns": [
{
"Content": ""
},
{
"Content": ""
},
{
"Content": ""
},
{
"Content": ""
},
{
"Content": ""
},
{
"Content": ""
}
],
"MoreInfoList": [
{
"Content": "3",
"MoreInfoTypesString": "LicenseFrom",
"MoreInfoTypesInt": 16
},
{
"Content": "3",
"MoreInfoTypesString": "LicenseTo",
"MoreInfoTypesInt": 32
},
{
"Content": "4.3",
"MoreInfoTypesString": "PricePeriod1",
"MoreInfoTypesInt": 64
}
],
"PricePlans": [
"O2"
],
"ProductName": ""
}
]
],
"RegulationsInfoLink": "http://www.test.aspx",
"ServiceType": "TST",
"SubTitle": "",
"SWDefault": "1",
"Title": ""
},
"User": {
"CurrentLicenseNumber": 0,
"CustomerID": "21670106",
"FirstName": "",
"LastName": "",
"RequestedLicenseNumber": "3",
"SubscriberPhoneNumber": "035448428",
"IdentityNumber": "058470",
"Email": "alexanrbe@gmail.com",
"RegistrationStatus": "NOTREGISTERED"
},
"SalePersonID": "3178364",
}
{
“验证令牌”:“1cc6cca8-44d5-4042-af37-de6a0d198d17”,
“AppID”:“TST”,
“以Chosen提案开展的活动”:{
“横幅”:[
{
“LocationCodeString”:“ManagerTab”,
“LocationCodeInt”:256,
“MediaUrl”:”
}
],
“代码”:“CAMP221”,
“CustomerInstruction”:“-1”,
“信息链接”:http://test.aspx",
“LobbySubTitle”:“,
“说客头衔”:“,
“MoreText”:“,
“NumOfColumns”:0,
“numorrows”:0,
“原始字符串”:“INT”,
“原创”:4,
“建议”:[
[
{
“设备代码”:[
"5455"
],
“功能代码”:“BE5455”,
“无效原因”:“,
“IsExistsMoreInfo”:没错,
“IsValid”:正确,
“LeadCode”:“3792956510”,
“ParamCombinationCode”:0,
“产品代码”:“OANTIVRP2”,
“ProductCombinationCode”:“0”,
“提议代码”:“29160001201C8F83661D5B82FD5F3603967588B7A72”,
“栏目”:[
{
“内容”:”
},
{
“内容”:”
},
{
“内容”:”
},
{
“内容”:”
},
{
“内容”:”
},
{
“内容”:”
}
],
“更多信息列表”:[
{
“内容”:“3”,
“MoreInfoTypesString”:“LicenseFrom”,
“MoreInfoTypesInt”:16
},
{
“内容”:“3”,
“MoreInfoTypesString”:“LicenseTo”,
“MoreInfoTypesInt”:32
},
{
“内容”:“4.3”,
“MoreInfoTypesString”:“PricePeriod1”,
“MoreInfoTypesInt”:64
}
],
“价格计划”:[
“氧气”
],
“产品名称”:”
}
]
],
“法规信息链接”:http://www.test.aspx",
“服务类型”:“TST”,
“副标题”:“,
“SWDefault”:“1”,
“标题”:”
},
“用户”:{
“CurrentLicenseNumber”:0,
“客户ID”:“21670106”,
“名”:“名”,
“姓氏”:“,
“RequestedLicenseNumber”:“3”,
“SubscriberPhoneNumber”:“035448428”,
“识别号”:“058470”,
“电子邮件”:alexanrbe@gmail.com",
“注册状态”:“未注册”
},
“SalePersonID”:“3178364”,
}
在序列化行上引发异常,如下所示:
var paramType = p_BindingContext.ModelType;
dynamic updatedParam = Activator.CreateInstance(paramType);
JsonReader reader = new JTokenReader(JObject.Parse
(p_ActionContext.Request.Content.ReadAsStringAsync().Result));
JObject jObject = JObject.Load(reader);
JsonSerializer serializer = new JsonSerializer();
serializer.Populate(jObject.CreateReader(), updatedParam);//Here the exception thorws
p_BindingContext.Model = updatedParam;
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type WebAPI.Models.Entities.Campaigns.CampaignObjects.ProposalItem' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
无法将当前JSON数组(例如[1,2,3])反序列化为WebAPI.Models.Entities.Campaigns.CampaignObjects.ProposalItem类型,因为该类型需要JSON对象(例如{“name”:“value”})才能正确反序列化。
要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”}),或将反序列化类型更改为数组或实现可从JSON数组反序列化的集合接口(例如ICollection、IList)类似列表的类型。还可以将JsonArrayAttribute添加到类型中,以强制它从JSON数组反序列化。
我花了几个晚上来解决这个异常,但没有找到任何解决方案
此外,该站点上的解决方案是受激励的,但不支持此类复杂问题
有人遇到这样的行为,可以帮忙吗
谢谢根据您的JSON数据,您需要映射的对象必须如下所示(当然,除非您有自己的JsonDeserializer):
公共类横幅
{
公共字符串位置CodeString{get;set;}
public int LocationCodeInt{get;set;}
公共字符串MediaUrl{get;set;}
}
通过Chosen提案开展的公共课堂活动
{
公共列表横幅{get;set;}
公共字符串代码{get;set;}
公共字符串CustomerI