【c# – 将名称/值对反序列化为对象】教程文章相关的互联网学习教程文章

c#-使用setter方法进行Json.Net反序列化【代码】

使用下面的示例是否可以在反序列化期间使用Json.Net中的AddChild Setter方法填充子代列表?public class Foo {private IList<Foo> _children;private Foo _parent;public Foo(){_children = new List<Foo>();}public string Name { get; set; }public IEnumerable<Foo> Children{get { return _children.AsEnumerable() }}public void AddChild(Foo child){child._parent = this;_children.Add(child);} }解决方法:您可以通过创建J...

c#-从json反序列化字典,可能没有Newtonsoft.Json吗?【代码】

帮我反序列化Dictionary对象. 举例来说,我上课了[Serializable] public class MyClass {/// <summary>/// some class variables/// </summary>public string variable_A;public decimal variable_B;/// <summary>/// constructor for example/// </summary>public MyClass(string s, decimal d){variable_A = s;variable_B = d;} }然后创建Dictionary-以字符串为键,以MyClass对象为值:Dictionary<string, MyClass> myDictionary =...

将对象值保持在XML序列化和反序列化(C#)之间【代码】

我有一个要尝试使用XMLSerializer进行序列化和反序列化的类.该类如下所示:namespace AutoCAD_Adapter {/// <summary>/// Test class meant to be serialized to test serialization methods/// </summary>[Serializable]public class SerializeTest// : ISerializable{#region class variablesprivate int x;private int y;#endregion#region Constructorspublic SerializeTest(int passedX, int passedY){this.x = passedX;this....

c#-Restsharp xml反序列化以列出而不更改模型名称【代码】

我的XML格式不是很好,但是需要使用RestSharp映射到List.我无法控制service / xml输出.到目前为止,我可以使用DeserializeAs(Name =“ name”)]属性来解决属性本身的问题.例如,public class Physician {[DeserializeAs(Name = "personId")]public string Id { get; set; }[DeserializeAs(Name = "fName")]public string FirstName { get; set; }[DeserializeAs(Name = "lName")]public string LastName { get; set; } }当我具有以下x...

c#-与XML反序列化IEnumerable类有关的错误【代码】

我正在尝试将HistoryRoot类序列化和取消序列化为这种XML格式:<?xml version="1.0"?> <HistoryRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Files><HistoryItem d="2015-06-21T17:40:42" s="file:///D:\cars.txt" /></Files><Folders><HistoryItem d="2015-06-21T17:40:42" s="D:\fc\Cars" /></Folders> </HistoryRoot>这是HistoryRoot,HistoryList和HistoryItem类...

C#与IXmlSerializable反序列化【代码】

我有这样的XML:<data> <audit_values><audit_value><channel>2</channel><week><mo_th>6,501698000000</mo_th><fr>8,414278000000</fr><sa>9,292674000000</sa><sun>8,551982000000</sun><holid>7,164605000000</holid></week></audit_value><audit_value><channel>1</channel><week><mo_th>6,501698000000</mo_th><fr>8,414278000000</fr><sa>9,292674000000</sa><sun>8,551982000000</sun><holid>7,164605000000</holid></we...

JavaScript-在C#中反序列化Paypal响应的正确方法【代码】

介绍 我正在为我的演示项目使用Paypal付款实现.当用户确认请求时,响应和请求以json格式接收(你们大多数人都知道). 代码设置 行动“解析”数据的地方string str = JObject.Parse(executedPayment.ConvertToJson()).ToString(Newtonsoft.Json.Formatting.Indented); var payerInfo = new JavaScriptSerializer().Deserialize<ResponseMappingObject.Payer_Info>(str);foreach(var item in payerInfo) { string abc = payerInfo.first...

c#-DataContract反序列化XML-同一元素中的元素和属性列表【代码】

我在c#ASP.NET环境中工作.我试图将以下XML元素反序列化为c#对象/类.元素深3层.<Availability><RecommendedSegment><Duration>1720</Duration><FareBasis>Y77OW</FareBasis><FlightSegment><DepDate>11 August</DepDate><DepTime>0830</DepTime><ArrDate>11 August</ArrDate><ArrTime>1110</ArrTime><DepDay>Mon</DepDay><ArrDay>Mon</ArrDay><DepAirport>LHR</DepAirport><DepAirportName>Heathrow</DepAirportName><DepCityName>L...

c#-如何基于元素的值反序列化xml到派生类?【代码】

例如,我有一个xml:<MyFruit><Fruit><Name>Apple</Name><Size>Big</Size></Fruit><Fruit><Name>Orange</Name><Price>10.00</Price></Fruit> </MyFruit>您可能会注意到,水果节点包含不同的元素,这是我的伤害:( 然后,我定义了以下类以容纳反序列化的对象:public class MyFruit {public List<Fruit> Fruits { get; set; } }public abstract class Fruit {public string Name { get; set; } }public class Apple : Fruit {public str...

c#-枚举属性,Web API,JSON反序列化和错别字【代码】

假设我们有一个像这样的课程:public class Person {[JsonConstructor]public Person(string name, DayOfWeek bornOnDay) => (Name, BornOnDay) = (name, bornOnDay);public string Name { get; protected set; }public DayOfWeek BornOnDay { get; protected set; } }还有一个这样的端点:[HttpPost] [Route("api/people")] public IHttpActionResult PostPerson([FromBody]List<Person> people) {// whatever }我注意到,如果我在...

如何创建C#类以反序列化以方括号开头和结尾的JSON字符串【代码】

我正在从API接收到JSON字符串,并想将其反序列化为C#对象,但无法正确获取类. 我尝试使用http://json2csharp.com/创建类,但无法解析JSON,但是https://jsonlint.com/表示JSON有效. 我也尝试运行JsonClassGeneratorLib,但这说明Unable to cast object of type Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject这似乎是一个问题,因为JSON括在[]方括号中.我相信这是有效的,但可以将其放入数组.我想我班上某个地方需要一个数...

c#-(反序列化)“二进制流不包含有效的BinaryHeader”错误?【代码】

我在“对象到字节不起作用”之前创建了一个帖子.我修复了用户说的问题,但仍然存在问题. 错误消息:找不到反序列化“ WindowsFormsApplication1.Form1项”类型的对象的构造函数.void start() {Item item = new Item();item.files.Add(@"test");byte[] b = ObjectToByteArray(item); Item k = Desriles(b);}[Serializable] public class Item : ISerializable {public Item(){files = new List<string>();Exclude = false;CleanEmpt...

C#中涉及引用时的序列化和反序列化【代码】

我有一个称为Manager的单例类,其中包含对象实例列表:static class Manager {static List<Foo> Foos = new List<Foo>(); }然后,我使用名为Meter的类来收集对象实例,该类使用对列表Foos中各项的引用:class Meter {public Foo MyFoo = null; }...public void CreateMeter(int UserChoice) {Meter MyMeter = new Meter();MyMeter.MyFoo = Manager.Foos[UserChoice]; }当应用程序保存项目文件时,它将序列化Foo中的Foo实例以及Meter的所...

c#-这是反序列化Facebook JSON字符串的好方法吗?【代码】

这是我第一次尝试反序列化Facebook返回的JSON字符串.我想确保将来的开发人员可以舒适地维护我的代码,所以我想知道这样做是否可行.我担心如果JSON字符串更改,那么我将需要重写一些类.从Facebook返回的JSON字符串是否可能会更改?例如,如果位置变成了另一个对象,我需要进行更改吗?using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Text; using System.Web; //...

c#-使用BinaryFormatter序列化和反序列化List>【代码】

假设我有List<object> mainList = new List<object>();它包含List<string> stringList = new List<string(); List<CustomClass> custList = new List<CustomClass>(); mainList.Add(stringList); mainList.Add(custList);序列化Stream stream; BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, mainList);反序列化Stream stream = (Stream)o; BinaryFormatter formatter = new BinaryFormatter();...

反序列化 - 相关标签