Tag: json.net

无法使用Json.net使用Complex键序列化Dictionary

我有一个自定义.net类型的字典作为其密钥。我正在尝试使用JSON.net将此字典序列化为JSON,但是它无法在序列化期间将密钥转换为适当的值。 class ListBaseClass { public String testA; public String testB; } —– var details = new Dictionary(); details.Add(new ListBaseClass { testA = “Hello”, testB = “World” }, “Normal”); var results = Newtonsoft.Json.JsonConvert.SerializeObject(details); var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary> results); 这个给我 – >“{\”JSonSerialization.ListBaseClass \“:\”Normal \“}” 但是,如果我将自定义类型作为字典中的值,它可以很好地工作 var details = new Dictionary(); details.Add(“Normal”, new ListBaseClass { testA = “Hello”, […]

使用自定义WCF正文反序列化而不更改URI模板反序列化

从这篇博文中 ,我能够创建一个使用JSON.NET序列化的自定义WCF IDispatchMessageFormatter 。 它有一点需要注意:与UriTemplate一起使用它并不一定按预期工作。 这是博客文章提供的实现: class NewtonsoftJsonDispatchFormatter : IDispatchMessageFormatter { private readonly OperationDescription od; private readonly ServiceEndpoint ep; private readonly Dictionary parameterNames = new Dictionary(); public NewtonsoftJsonDispatchFormatter(OperationDescription od, ServiceEndpoint ep, bool isRequest) { this.od = od; this.ep = ep; if (isRequest) { int operationParameterCount = od.Messages[0].Body.Parts.Count; if (operationParameterCount > 1) { this.parameterNames = new […]

JToken:获取原始/原始JSON值

有没有办法从JToken获取原始/原始JSON值? 问题: var data = JObject.Parse(@”{ “”SimpleDate””:””2012-05-18T00:00:00Z””, “”PatternDate””:””2012-11-07T00:00:00Z”” }”); var value = data[“SimpleDate”].Value(); 该value现在是05/18/2012 00:00:00但我需要原始字符串2012-05-18T00:00:00Z 。 有没有办法获得这个原始价值? 另外,我无法改变创建JObject的方式(例如更改设置),因为它作为参数传递到我的类中… (参考: 最初的NJsonSchema问题 )

使用每种类型的不同格式在同一类中序列化多个DateTime属性

我有一个具有两个DateTime属性的类。 我需要使用不同的格式序列化每个属性。 我该怎么做? 我试过了: JsonConvert.SerializeObject(obj, Formatting.None, new IsoDateTimeConverter {DateTimeFormat = “MM.dd.yyyy”}); 此解决方案对我不起作用,因为它将日期格式应用于所有属性。 有没有办法用不同的格式序列化每个DateTime属性? 也许有一些属性?

如何使用Json.Net使用自定义键序列化/反序列化字典?

我有以下类,我用作字典中的键: public class MyClass { private readonly string _property; public MyClass(string property) { _property = property; } public string Property { get { return _property; } } public override bool Equals(object obj) { MyClass other = obj as MyClass; if (other == null) return false; return _property == other._property; } public override int GetHashCode() { […]

将JSON的一部分替换为其他部分(使用字符串标记)

我有一个我无法解决的问题,我在不同的地方寻求和寻求,但我仍然没有找到它。 看到这段代码: //I have 2 json to merge var json = @”{‘client’: {‘name’: ‘Should NO CHANGE’, ‘lastname’: ”, ’email’: ‘asd@asd.com’, ‘phone’: ”, ‘birthday’: ”, ‘married’: false, ‘spouse’: {‘name’: ”, ‘lastname’: ”} } }”; var json2 = @” {‘client’: {‘name’: ‘aaaa’, ‘lastname’: ‘bbbb’, ’email’: ‘cccccc@ccccc.com’, ‘phone’: ”, ‘birthday’: ”, ‘married’: false, ‘spouse’: {‘name’: ‘dddddd’, ‘lastname’: ‘eeeee’} […]

如何将Json数组转换为c#中的对象列表

我有Json字符串,如下所示 { “JsonValues”:{ “id”: “MyID”, “values”: { “value1”:{ “id”: “100”, “diaplayName”: “MyValue1” }, “value2”:{ “id”: “200”, “diaplayName”: “MyValue2” } } } } 我想将Json字符串转换为以下类 class ValueSet { [JsonProperty(“id”)] public string id { get; set; } [JsonProperty(“values”)] public List values { get; set; } } class Value { public string id { get; set; } public string […]

如果我事先不知道密钥,如何解析C#中的JSON对象?

我有一些看起来像这样的JSON数据: { “910719”: { “id”: 910719, “type”: “asdf”, “ref_id”: 7568 }, “910721”: { “id”: 910721, “type”: “asdf”, “ref_id”: 7568 }, “910723”: { “id”: 910723, “type”: “asdf”, “ref_id”: 7568 } } 我如何使用JSON.net解析这个? 我可以先这样做: JObject jFoo = JObject.Parse(data); 我需要能够遍历此列表中的每个对象。 我希望能够做到这样的事情: foreach (string ref_id in (string)jFoo[“ref_id”]) {…} 要么 foreach (JToken t in jFoo.Descendants()) { Console.WriteLine((string)t[“ref_id”]); } 但当然这不起作用。 如果您在编写代码时知道密钥,那么所有示例都很有用。 […]

遵循Ruby属性命名约定的C#JSON.NET约定?

我正在使用http://json.codeplex.com/ ,我正在谈论基于Ruby的Rest API。 问题是大多数属性都有一个ruby下划线命名约定。 我想知道是否有人知道一种方式,以便我可以避免添加大量的JsonProperty。 例如,我想避免添加JsonProperty属性并在序列化程序设置中内置一个约定,以便它知道在.NET命名约定中尝试使用下划线映射属性:) public class Member { [JsonProperty(PropertyName = “avatar_url”)] public string AvatarUrl { get; set; } [JsonProperty(PropertyName = “twitter_screen_name”)] public string TwitterScreenName { get; set; } [JsonProperty(PropertyName = “website_url”)] public string WebSiteUrl { get; set; } }

如何在反序列化过程中以编程方式选择构造函数?

我想反序列化以下列方式序列化的System.Security.Claims.Claim对象: { “Issuer” : “LOCAL AUTHORITY”, “OriginalIssuer” : “LOCAL AUTHORITY”, “Type” : “http://my.org/ws/2015/01/identity/claims/mytype”, “Value” : “myvalue”, “ValueType” : “http://www.w3.org/2001/XMLSchema#string” } 我得到的是JsonSerializationException : 无法找到用于System.Security.Claims.Claim类型的构造函数。 一个类应该有一个默认的构造函数,一个带参数的构造函数或一个用JsonConstructor属性标记的构造函数。 经过一些调查后,我终于理解了上面消息中一个的含义:JSON反序列化器找不到正确的构造函数 – 在Claim类型的情况下 – 带有参数的多个构造函数 (尽管存在一个构造函数,其参数完全匹配以上属性)。 有没有办法告诉反序列化器选择哪个构造函数而不将JsonConstructor属性添加到该mscorlib类型? 几年前, Daniel Halan用Json.NET的补丁解决了这个问题。 有没有办法在不修改Json.NET的情况下解决这个问题?