Tag: 反序列化

在反序列化JSON String期间处理名称空间更改

我有2个应用程序在redis服务器的帮助下相互通信,在我的第一个应用程序中,我能够序列化和反序列化以及以下类型的对象 { “$type”: “System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib], [OPCMessagingService.Resource, OPCMessagingService]], mscorlib”, “71”: { “$type”: “OPCMessagingService.Resource, OPCMessagingService”, “SiteID”: 2, “ResourceID”: 71, “ProcessOrder”: “001000380873”, “CurrentStatus”: 0, “CycleTime”: 55, “DeviceList”: [ { “$type”: “OPCMessagingService.StackLight, OPCMessagingService”, “ResourceId”: 71, “DeviceIp”: “10.142.117.7”, “ComPort”: “COM14” }, { “$type”: “OPCMessagingService.LED, OPCMessagingService”, “ResourceId”: 71, “DeviceIp”: “10.142.117.3”, “ComPort”: “COM10”, “IsMessageDelivered”: false, “RetryCount”: 3, “IsRetryRequired”: true, “_messageHeader”: “:*HF601011471”, “_messageText”: […]

从序列化ViewModel还原时,Combox SelectedItem不适用

在恢复ViewModel(使用Json.Net序列化)时使用C#WPF和MVVM模式时,我遇到了一个奇怪的问题。 该软件的想法是 – 关闭窗口时 – 将当前Viewmodel状态保存在json文件中。 在下一次启动时,应用程序只是为json提供支持。 如果有文件,则将其反序列化并恢复ViewModel(设置公共属性)。 如果没有文件,则创建viewmodel并设置默认值。 现在我的问题是,当使用json文件恢复它时,包含自定义类型列表的combobox,combobox具有值但没有SelectedItem 。 在创建viewmodel实例并使用默认值初始化公共属性时(通过后面的代码执行此操作),一切都很好。 以下是一些代表“错误”的代码:View 代码背后 using System; using System.Windows; using System.IO; using Newtonsoft.Json; namespace CrazyWpf { public partial class MainWindow : Window { private DemoViewModel dvm; public MainWindow() { InitializeComponent(); this.dvm = (DemoViewModel)this.rootElement.DataContext; } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), “settings.json”); […]

c#:使用命名空间将xml反序列化为clr对象

我需要一些XmlSerializer的帮助。 我必须遵循xml片段: tag:blogger.com,1999:blog-4233645339430781865.archive 2012-10-22T07:00:02.139+03:00 Code !t Емил Табаков noreply@blogger.com Blogger tag:blogger.com,1999:blog-4233645339430781865.post-513753811167440871 2012-10-12T11:22:35.759+03:00 2012-10-12T11:22:35.759+03:00 Great post indeed. I really like that you are prov… Great post indeed. I really like that you are providing information on .NET for freshers , Being enrolled at http://www.wiziq.com/course/57-fresher-training-projects i found your information very helpful indeed. Thanks for it. sarabjeet http://www.blogger.com/profile/11223974173581186160 […]

解析具有不一致字段名称的JSON字符串

我在反序列化以下JSON结构时遇到问题。 每个节点都包含一个ID和带有值的多语言代码。 语言属性的数量不一致。 但我需要这些值作为具有语言字段和值字段的对象列表。 [{ “id”:”w_312457″, “eng”:”deep-fat frying”, “ger”:”Frittieren” }, { “id”:”w_312458″, “fre”:”frying”, “ger”:”braten (in Öl)” }, { “id”:”w_312477″, “ger”:”perfekt gewürzte und abgestimmte Soße ” }] 我尝试使用JsonPropertyName属性,我得到了ID值。 但对于lang节点,我不知道我可以指定的名称。 以下是我的CLR对象, public class Word { public string Id { get; set; } // This is working // What can I specify here. I need a list of […]

System.MissingMethodException:json数组的反序列化时出错

反序列化jsonString时出错。 错误是Type ‘oodleListingsUser’ is not supported for deserialization of an array. 我的反序列化代码是 string jsonString = new WebClient().DownloadString(“http://api.oodle.com/api/v2/listings?key=TEST&region=region_value&category=category_value&format=json”); JavaScriptSerializer ser = new JavaScriptSerializer(); jsonOodleApi p = ser.Deserialize(jsonString); 我对jsonOodleApi的定义是 public class jsonOodleApi { public oodleCurrent current; public oodleListings[] listings; public oodleMeta meta; public string state { get; set; } } 定义oodleCurrent和oodleMeta我不给予因为它的完美! oodleListings的定义是 public class oodleListings { public […]

问题将JSON反序列化为List

我有一个问题将JSON字符串反序列化为List TCProject如下: [JsonObject(MemberSerialization.OptIn)] public class TCProject { public override string ToString() { return Name; } [JsonProperty(PropertyName = “archived”)] public bool Archived { get; set; } [JsonProperty(PropertyName = “description”)] public string Description { get; set; } [JsonProperty(PropertyName = “href”)] public string Href { get; set; } [JsonProperty(PropertyName = “id”)] public string Id { get; set; } […]

如何反序列化JSON数组并忽略根节点?

我有来自服务器的下一个响应 – {“response”:[{“uid”:174952xxxx,”first_name”:”xxxx”,”last_name”:”xxx”}]} 我试图以下一种方式反序化 – JsonConvert.DeserializeObject(json); 其中T = VkUser的列表,但是我收到了错误。 [JsonObject] public class VkUser { [JsonProperty(“uid”)] public string UserId { get; set; } [JsonProperty(“first_name”)] public string FirstName { get; set; } [JsonProperty(“last_name”)] public string LastName { get; set; } } 我总是试着 public class SomeDto // maybe Response as class name will fix it but I don’t […]

JSON.net反序列化

我有一个JSON编码的字符串。 我也有C#类: class Pupu{ public static List plist = new List(); public string user {get; set;} public string number {get; set;} } [{“user”:”27FFBADD7284E5CB98EAC45559589E28FDDDC3AD”,”number”:”62827″},{“user”:”27FFBADD7284E5CB98EAC45559589E28FDDDC3AD”,”number”:”30460″}, {“user”:”0D27D44D40C5185423078B3C93B3E6B596AD21A0″,”number”:”25143″}, {“user”:”0D27D44D40C5185423078B3C93B3E6B596AD21A0″,”number”:”22776″}, {“user”:”27FFBADD7284E5CB98EAC45559589E28FDDDC3AD”,”number”:”19755″}] 我希望它: plist = JsonConvert.DeserializeObject<List>(stringAbove); 我一直在: Attempt to access the method failed: System.Collections.Generic.List`1..ctor(); 谢谢,我现在正在寻找几个小时。 (我对C#很新)。

在没有适当的REST-Api的情况下测试RestSharp的反序列化

编辑:问题的解决方案可以在John Sheehan的第一条评论中找到! 我想在我的项目中使用Restsharp作为Rest-Client。 由于REST服务器尚未运行,我想在没有服务器的情况下测试客户端。 我主要关注的是返回XML-Response的反序列化。 是否可以在没有适当的RestSharp.RestResponse的情况下使用RestSharp反序列化XML? 我试过这样的: public void testDeserialization() { XmlDeserializer d = new XmlDeserializer(); RestSharp.RestResponse response = new RestSharp.RestResponse(); string XML = @”Some textAnother textEven more text”; response.Content = XML; d.RootElement = “Response”; Response r = d.Deserialize(response); } public class Response { public string Item1 { get; set; } public string Item2 { […]

如何将此JSON反序列化为对象?

我正在尝试使用JSON.Net将JSON对象反序列化为C#对象。 我想要创建的对象是MonthlyPerformance ,它包含Type列表,其中包含Categories列表,而MonthlyPerformance列表又包含一个Funds列表。 它们被定义为: public class MonthlyPerformance { public List Types { get; set; } } public class Type { public int Id { get; set; } public string CountryId { get; set; } public string Name { get; set; } public List Categories { get; set; } public Type() { } } public class […]