Tag: 反序列化

Json.NET – CustomCreationConverter中单个属性的默认反序列化行为

在下面的场景中,当遇到我反序列化的类型中存在的JSON属性时,如何让CrazyItemConverter像往常一样继续? 我有一些看起来像这样的JSON: { “Item”:{ “Name”:”Apple”, “Id”:null, “Size”:5, “Quality”:2 } } JSON被反序列化为一个看起来像这样的类: [JsonConverter(typeof(CrazyItemConverter))] public class Item { [JsonConverter(typeof(CrazyStringConverter))] public string Name { get; set; } public Guid? Id { get; set; } [JsonIgnore] public Dictionary CustomFields { get { if (_customFields == null) _customFields = new Dictionary(); return _customFields; } } … } CrazyItemConverter填充已知属性的值,并将未知属性放在CustomFields中。 其中的ReadJson看起来像这样: public […]

C#从整数反序列化枚举

是否可以从c#中的int反序列化枚举。 例如,如果我有以下课程: class Employee { public string Name { get; set;} public int EmployeeTypeID { get; set;} } 我可以轻松地从XML创建它 Joe Bloggs 1 使用这样的东西: Employee employee = (Employee)new XmlSerializer(typeof(Employee)).Deserialize(XmlReader); 由于涉及的工作很少,这允许我使用一个通用服务,我可以通过将select命令,连接字符串和类型输入到并检索对象数组而不需要进一步映射来为所有数据库对象使用。 但是我已经忘记了枚举。 假设现在而不是整数EmployeeType是一个枚举: public enum EmployeeTypeEnum { Admin = 1, Sales = 2 } 所以我的class级成了: class Employee { public string Name { get; set;} public EmployeeTypeEnum EmployeeTypeID […]

自动创建的用于xml反序列化的C#类不起作用

我正在努力为这个xml创建反序列化类: John Johnos Some Street 1 24 success 我试图使用自动创建的代码(在VisualStudio 12中:编辑 – >选择性粘贴 – >将XML粘贴为类): /// [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“code”)] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = “http://schemas.xmlsoap.org/soap/envelope/”)] [System.Xml.Serialization.XmlRootAttribute(Namespace = “http://schemas.xmlsoap.org/soap/envelope/”, IsNullable = false)] public partial class Envelope { private EnvelopeBody bodyField; private string encodingStyleField; /// public EnvelopeBody Body { get { return this.bodyField; } set { this.bodyField = value; […]

如何基于Json反序列化的JArray数据创建DataTable?

我已经停止了JArray数据。 我需要根据Jarray中的列名动态创建一个数据表,之后我需要插入数据。 你能帮我做一下这个操作吗? {[ { “PID”: 3, “FirstName”: “parveen”, “LastName”: “a”, “Gender”: “male” }, { “PID”: 8, “FirstName”: “ramarao”, “LastName”: “M”, “Gender”: “male” } ]} 提前致谢 普尔纳

序列化和反序列化Visual Studio解决方案文件 – 或以编程方式编辑?

我想以编程方式添加和删除项目,解决方案文件夹和其他项目(如资源文件)到解决方案,但我不确定什么是最好的方法。 对于那些不了解的人,高度简化; 这是一个suution文件(.sln)通常的样子: Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 Project(“{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}”) = “project name”, “projectpath\name.csproj”, “{785ECC80-AF1B-4FBC-B97B-2EC43B7E81E8}” EndProject Global GlobalSection(ProjectConfigurationPlatforms) = postSolution {785ECC80-AF1B-4FBC-B97B-2EC43B7E81E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU EndGlobalSection EndGlobal 我不确定这是否是一个真正的开放标准,如xml(它是?),但它看起来像是基础团队制作了自己的xml版本。 (不是恭维) 那么,无论如何,将这些对象序列化和反序列化的最佳方法是什么? 我正在考虑制作自己的IFormatter,但这看起来相当复杂。

使用带有JSON.Net的自定义反序列化器反序列化JSON

我有这样的JSON: { “MobileSiteContents”: { “au/en”: [ “http://www.url1.com”, “http://www.url2.com”, ], “cn/zh”: [ “http://www.url2643.com”, ] } } 我正在尝试将其反序列化为IEnumerable类,如下所示: public class MobileSiteContentsContentSectionItem : ContentSectionItem { public string[] Urls { get; set; } } public abstract class ContentSectionItem { public string Culture { get; set; } } 那可能吗? 我意识到我可能需要为此使用Custom JsonConverter,但找不到任何示例。 我开始编写一个使用JObject.Parse进行转换的方法,但不确定这是否是正确/最有效的路径: public IEnumerable Parse(string json) { var jobject = […]

反序列化为double时,JsonConvert会抛出一个“非有效整数”exception

当我尝试从JSON字符串反序列化为对象时,我得到一个exception。 Input string ‘46.605’ is not a valid integer. Path ‘LatitudeCenter’ 这真的很奇怪,因为JsonConvert尝试将属性反序列化为整数,但它实际上是一个double而不是整数 。 我已经检查了我的Web API项目。 我的类中的属性是web项目中的double和same。 我在web asp项目中使用的代码: using (var client = new HttpClient()) { client.BaseAddress = new Uri(“myWebApiHostedUrl”); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”)); // Get the response HttpResponseMessage response = await client.GetAsync(“api/NewMap/?SouthLatitude=46.600&WestLongitude=7.085&NorthLatitude=46.610&EastLongitude=7.095&Width=900&Height=900&isVoxelMap=true”); string jsonData = response.Content.ReadAsStringAsync().Result; //Exception here NewMap dataMewMap = JsonConvert.DeserializeObject(jsonData, new JsonSerializerSettings() { Culture […]

如何只将大型xml文件的一部分反序列化为c#类?

我已经阅读了一些关于如何反序列化xml的post和文章,但仍然没有想出我应该编写代码以满足我的需求的方式,所以..我为另一个关于反序列化xml的问题道歉)) 我有一个大的(50 MB)xml文件,我需要反序列化。 我使用xsd.exe来获取文档的xsd模式,而不是自动生成我放入项目的c#classes文件。 我想从这个xml文件中获取一些(不是全部)数据并将其放入我的sql数据库中。 这是文件的层次结构(简化,xsd非常大): public class yml_catalog { public yml_catalogShop[] shop { /*realization*/ } } public class yml_catalogShop { public yml_catalogShopOffersOffer[][] offers { /*realization*/ } } public class yml_catalogShopOffersOffer { // here goes all the data (properties) I want to obtain )) } 这是我的代码: 第一种方法: yml_catalogShopOffersOffer catalog; var serializer = new XmlSerializer(typeof(yml_catalogShopOffersOffer)); var […]

如何以编程方式从动态JObject获取属性

我正在使用NewtonSoft JObject解析JSON字符串。 如何以编程方式从动态对象获取值? 我想简化代码,不要为每个对象重复自己。 public ExampleObject GetExampleObject(string jsonString) { ExampleObject returnObject = new ExampleObject(); dynamic dynamicResult = JObject.Parse(jsonString); if (!ReferenceEquals(dynamicResult.album, null)) { //code block to extract to another method if possible returnObject.Id = dynamicResult.album.id; returnObject.Name = dynamicResult.album.name; returnObject.Description = dynamicResult.albumsdescription; //etc.. } else if(!ReferenceEquals(dynamicResult.photo, null)) { //duplicated here returnObject.Id = dynamicResult.photo.id; returnObject.Name = dynamicResult.photo.name; returnObject.Description […]

使用MongoDB C#Driver反序列化多态类型

假设,我有一个基类 public class Node{ public ObjectId Id; public String nodeName; public ObjectId parentNode; } 和2个派生类 public class PlotNode:Node{ public ObjectId Id; public String plotDetail; } public class EndNode:Node{ public ObjectId Id; public int resultCode; } 序列化的所有3个类的几个对象都在数据库中。 并且只有我拥有的数据是ObjectId的列表,并且只有这些ID的知识是它们肯定是Node ids,但是它们在反序列化时间内不知道它们是Node,PlotNode还是EndNode。 我用它来反序列化: var collection = db.GetCollection(“nodes”); var query = Query.EQ(e => e.Id, id); Node node = collection.FindOne(query); 最后我得到节点,而不是实际的PlotNodes或EndNodes。 […]