我需要反序列化一个对象。我有一个请求类;
public class LegacyDataRequest
{
public string RowId { get; set; }
public string SchemaName { get; set; }
public string TableName { get; set; }
public string SelectStatement { get; set; }
}
这是响应类;
public class LegacyDataResponse
{
public List<KeyData> List { get; set; }
}
我这样反序列化;
var res = Newtonsoft.Json.JsonConvert.DeserializeObject<List<LegacyDataResponse>>(response.Substring(1, response.Length - 2));
这是回应;
[{"Key":"MUSTERI_NO","Data":"26594"},{"Key":"ADRES_KOD","Data":"E"},{"Key":"ADRES","Data":"DENİZ SEMA Site: 3"},{"Key":"SEMT","Data":"DUMLUPINAR"},{"Key":"IL_KOD","Data":"034"},{"Key":"POSTA_KOD","Data":""},{"Key":"ULKE_KOD","Data":"TR"},{"Key":"EMAIL","Data":""},{"Key":"ILK_GECERLILIK_TARIHI","Data":"7/28/2009 12:00:00 AM"},{"Key":"SON_GECERLILIK_TARIHI","Data":""},{"Key":"ILCE_KOD","Data":"10"},{"Key":"YARATAN_KULLANICI_KODU","Data":""},{"Key":"YARATILDIGI_TARIH","Data":""},{"Key":"EXTRE_ADRES_KOD_F","Data":"E"},{"Key":"DAHILI_NO","Data":""},{"Key":"ISYERI_UNVANI","Data":""},{"Key":"ADRES_TEYIT","Data":"E"},{"Key":"ADRES_TEYIT_TARIHI","Data":"9/21/2013 12:00:00 AM"},{"Key":"TELEFON_TEYIT","Data":"E"},{"Key":"TELEFON_TEYIT_TARIHI","Data":"9/25/2013 12:00:00 AM"},{"Key":"FAX_TEYIT","Data":"H"},{"Key":"FAX_TEYIT_TARIHI","Data":""},{"Key":"EMAIL_TEYIT","Data":"E"},{"Key":"EMAIL_TEYIT_TARIHI","Data":"9/25/2013 12:00:00 AM"},{"Key":"CONTACT_MOBILE_NUMBER","Data":"1"},{"Key":"CONTACT_EMAIL","Data":"1"},{"Key":"DIGER1_TEL_ALAN_KOD","Data":""},{"Key":"DIGER1_TEL_NO","Data":""},{"Key":"DIGER2_TEL_ALAN_KOD","Data":""},{"Key":"DIGER2_TEL_NO","Data":""},{"Key":"TEL_ALAN_KOD","Data":"216"},{"Key":"TEL_NO","Data":""},{"Key":"GSM_ALAN_KOD","Data":"532"},{"Key":"GSM_NO","Data":""},{"Key":"FAX_ALAN_KOD","Data":""},{"Key":"FAX_NO","Data":""}]
我收到了错误;
无法反序列化当前的JSON对象(例如{“name”:“value”}) 进入类型 'System.Collections.Generic.List`1 [BankPozitif.CRM.Library.ZeusApi.LegacyDataResponse]' 因为该类型需要一个JSON数组(例如[1,2,3])来反序列化 正确。
要修复此错误,请将JSON更改为JSON数组(例如 [1,2,3])或更改反序列化类型,使其成为正常的.NET type(例如,不是像整数这样的基本类型,不是集合类型 像数组或List一样,可以从JSON对象反序列化。 JsonObjectAttribute也可以添加到类型中以强制它 从JSON对象反序列化。
我该如何解决?
答案 0 :(得分:0)
我认为问题出在这里
{"Key":"ADRES","Data":"DENİZ SEMA Site: 3"}
将其更改为
{"Key":"ADRES","Data":"DENİZ SEMA", "Site": 3"}
答案 1 :(得分:0)
我找到了解决方案。我删除了“.SubString()”,问题解决了。
答案 2 :(得分:0)
您反序列化为错误的类型。
public class Item
{
public string Key { get; set; }
public string Data { get; set; }
}
var res = JsonConvert.DeserializeObject<List<Item>>(response));