Tag: 反序列化

在C#中,当一个字段可能是字符串或字符串数​​组时,如何反序列化此json?

我有一个asp.net-mvc网站,我正在从数据库中读取Json字符串。 以下是DB中的以下json。 它可能看起来像这样: {“description”: “Test”, “contacts”: [“joe@gmail.com”, “bill@yahoo.com”], “enabled”: true} 或这个: {“description”: “Test”, “contacts”: “joe@gmail.com, bill@yahoo.com”, “enabled”: true} 如您所见,联系人字段是: 一个字符串(用逗号分隔的项目) 一串字符串。 我想转换为这个类: public class MyJob { public string description; public string[] contacts; public string enabled; } 当我尝试使用JavascriptSerializer()仅指定一个字符串(将上面的内容更改为:public string contacts;)时: var serializer = new JavaScriptSerializer(); string contacts = serializer.Deserialize(theAboveJsonString).contacts; 我在它的数组的情况下得到这个错误: 类型’System.String’不支持反序列化数组。 什么是反序列化处理以下情况的最佳方法: 一个字符串 一串字符串。 对于联系人字段。 我很乐意提出所需的任何条件逻辑。 […]

使用Json.net将JSON数组中的多个项添加到C#中的对象

谁能告诉我如何反序列化包含多个属性的对象? 鉴于下面的场景,代码工作正常。 public ActionResult Index() { string json = @”{“”name””: “”Person 2″”,””email””: “”example@example.com””}”; var emp = JsonConvert.DeserializeObject(json); Response.Write(emp.name + emp.email); return View(); } public class Person { public string name { get; set; } public string email { get; set; } } 但是,如果数组包含多个项目,我该怎么做,例如 string json = @”{“”data””: [{“”name””: “”Person 1″”,””email””: “”test@test.com””},{“”name””: “”Person 2″”,””email””: “”example@example.com””}]}”; 提前致谢 […]

使用Json.NET反序列化复杂对象

我需要反序列化从grogle maps api返回的这个json: { “destination_addresses”: [ “Via Medaglie D’Oro, 10, 47121 Forlì FC, Italia”, “Via Torino, 20123 Milano, Italia”, “Via Guglielmo Marconi, 71, 40121 Bologna, Italia”, “Via Irnerio, 40126 Bologna, Italia” ], “origin_addresses”: [ “Via Medaglie D’Oro, 10, 47121 Forlì FC, Italia”, “Via Torino, 20123 Milano, Italia”, “Via Guglielmo Marconi, 71, 40121 Bologna, Italia”, “Via […]

如何在不调用构造函数的情况下反序列化类?

我在我的WCF数据服务中使用Json.NET。 这是我的课程(简化): [DataContract] public class Component { public Component() { // I’m doing some magic here. } } 如何在不使用JsonConvert.DeserializeObject调用构造函数的情况下反序列化该类? 对不起,如果不清楚,随时提问。

将JSON反序列化为多个C#子类之一

我有一个看起来像这样的json结构: “list”:[ { “type”:”link”, “href”:”http://google.com” }, { “type”:”image”, “src”:”http://google.com/logo.png” }, { “type”:”text”, “text”:”some text here” }, ] 我想将其反序列化为对象列表,其中每个对象都是基类的子类。 列表中的每个项目都有不同的属性(href,src,text),因此我不能使用相同的类来获取一个。 相反,我想要一个普通类的三个子类。 JSON列表中每个项的type属性可用于决定使用哪个子类。 例如,我可以拥有以下类 public Item{ public string type {get; set;} } public LinkItem : Item { public string href {get; set;} } public ImageItem : Item { public string src {get; set;} } public TextItem : […]

在WebAPI C#中请求反序列化期间捕获exception#

我正在使用WebAPI v2.2,我正在使用[FromBody]属性将WebAPI反序列化到对象上。 反序列化的目标类在内部方法上有[OnDeserialized]属性,如下所示: [OnDeserialized] internal void OnDeserialisedMethod(StreamingContext context) { // my method code } 我知道这个方法中的代码存在问题,我已经逐步完成并找到了它。 对我来说问题是我完全没有例外。 发生的事情是这个方法被跳出来,exception似乎被忽略了。 我的控制器操作被调用,我的目标对象未正确填充,因为此序列化方法未正确执行。 我的问题是; 如何捕获WebAPI中反序列化期间发生的exception?

xsi:type属性搞乱了C#XML反序列化

我使用XSD.exe根据XML模式(.xsd文件)自动生成C#对象。 我正在反序列化OpenCover输出,但其中一个部分类没有正确生成。 这是导致exception的行: 这是MethodPoint类的缩短版本: [System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”, “4.0.30319.33440”)] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“code”)] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class CoverageSessionModulesModuleClassesClassMethodsMethodMethodPoint { private string vcField; private string uspidField; private string ordinalField; private string offsetField; private string slField; private string scField; private string elField; private string ecField; private string becField; private string bevField; private string fileidField; } 现在我已经浏览了很多.xml文件,但OpenCover输出文件是唯一一个在属性中包含冒号的文件。 MethodPoint对象也是唯一包含属性冒号的对象。 如您所见,该类不包含xsi:type属性,我知道只是添加它将因冒号而无效。 你如何处理xsi前缀? 这是从其中一个OpenCover XML文件生成的原始.xsd

多个JsonProperty名称分配给单个属性

我有两种格式的JSON,我想反序列化为一个类。 我知道我们不能将两个[JsonProperty]属性应用于一个属性。 能告诉我一个实现这个目标的方法吗? string json1 = @” { ‘field1’: ‘123456789012345’, ‘specifications’: { ‘name1’: ‘HFE’ } }”; string json2 = @” { ‘field1’: ‘123456789012345’, ‘specifications’: { ‘name2’: ‘HFE’ } }”; public class Specifications { [JsonProperty(“name1”)] public string CodeModel { get; set; } } public class ClassToDeserialize { [JsonProperty(“field1”)] public string Vin { get; set; } [JsonProperty(“specification”)] […]

在反序列化时如何忽略JSON对象数组中的空白数组?

我正在使用Json.NET反序列化JSON。 如何在反序列化期间忽略在对象数组内意外发生的空白数组 ? 我在本网站http://json.parser.online.fr/上测试了来自第三方的以下JSON,确认其格式正确: { “total_events”: 3551574, “json.appID”: [ { “count”: 3551024, “term”: 1 }, { “count”: 256, “term”: 2 }, [] /* <—– I need to ignore this empty array */ ], "unique_field_count": 2 } 我想将它反序列化为以下模型: public class RootObject { [JsonProperty(“total_events”)] public int TotalEvents { get; set; } [JsonProperty(“json.appID”)] public List AppIds { get; […]

如何防止单个对象属性在字符串时转换为DateTime

这是我必须使用的模型的简化版本: class InputModel { public string Name { get; set; } public object Value { get; set; } } 以及控制器的相关部分: class Controller : ApiController { [HttpPut] public async Task Update([FromBody]InputModel model) { //implementation } } InputModel类的Value属性可以是任何类型,它将在以后知道的类型中,在模型发送到的一段遗留代码中,我无法控制。 我在请求正文中使用以下json发生的问题: { “name”: “X”, “value”: “2001-10-17T13:55:11.123” } 默认行为是解析此json,以便将Value属性转换为DateTime。 但是,DateTimes的处理方式与遗留代码中的字符串截然不同,并且在处理后数据会丢失(例如:在持久保存到数据库时删除毫秒部分)。 因此,当稍后请求相同的值时,返回的值为“2001-10-17T13:55:11”(缺少毫秒)。 当然我可以通过在我的web api配置中全局设置来解决这个问题: httpConfiguration.Formatters.JsonFormatter.SerializationSettings.DateParseHandling = DateParseHandling.None; 但这样做也会禁用解析DateTimes以用于其他方法和控制器中的模型,这些方法和控制器具有需要默认行为的模型。 我正在寻找的是类似下面的(想象的)代码: class InputModel […]