json.net

How to Parse Json children in VB.NET Newtonsoft

偶尔善良 提交于 2019-12-29 07:46:30
问题 I am having touble parsing Json using VB.NET using the Newtonsoft Json.Net library Json Data --------- { "CC": "sample.cc@emailDomain.com", "CcFull": [ { "Email": "sample.cc@emailDomain.com", "Name": "John Sample" }, { "Email": "another.cc@emailDomain.com", "Name": "Mike Sample" } ], "FromFull" : { "Email": "myUser@theirDomain.com", "Name": "John Doe" } } I can get a valid JObject thus: Dim o As JObject = JObject.Parse(strJson) Then I can get list of a JTokens and iterate through them and

Serializing foreign languages using JSON.Net

自作多情 提交于 2019-12-29 07:35:13
问题 I want to serialize a .NET object to JSON which contains foreign language strings such as Chinese or Russian. When i do that (using the code below) in the resulting JSON it encodes those characters which are stored as strings as "?" instead of the requisite unicode char. using Newtonsoft.Json; var serialized = JsonConvert.SerializeObject(myObj, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, Formatting = Newtonsoft.Json.Formatting.Indented }); Is there a way to use the

How to cast JObject in JSON.Net to T

两盒软妹~` 提交于 2019-12-29 06:39:21
问题 I know that I can use JsonConvert.DeserializeObject<T>(string) , however, I need to peek into the object's _type (which may not be the first parameter) in order to determine the specific class to cast to. Essentially, what I am wanting to do is something like: //Generic JSON processor for an API Client. function MyBaseType ProcessJson(string jsonText) { var obj = JObject.Parse(jsonText); switch (obj.Property("_type").Value.ToString()) { case "sometype": return obj.RootValue<MyConcreteType>();

Json.NET Dictionary<string,T> with StringComparer serialization

夙愿已清 提交于 2019-12-29 05:47:04
问题 I have a dictionary Dictionary<string, Dictionary<string, object>> . Both the outer dictionary and the inner one have an equality comparer set(in my case it is StringComparer.OrdinalIgnoreCase ). After the dictionary is serialized and deserialized the comparer for both dictionaries is not set to StringComparer.OrdinalIgnoreCase . If you have control over the creation of the dictionaries in your code, you can create a class inherited from the dictionary and set comparer in the default

Issue using Json.Net to parse JSON to a DataSet

时光怂恿深爱的人放手 提交于 2019-12-29 01:56:06
问题 I've been trying to parse a block of json using json.net (https://dl.dropboxusercontent.com/u/2976553/json) but it fails stating that there is text after the json object. However, if you look at where it throws the exception if (checkAdditionalContent) { if (reader.Read() && reader.TokenType != JsonToken.Comment ) throw new JsonSerializationException("Additional text found in JSON string after finishing deserializing object."); } I checked the TokenType, and it is an EndObject which doesn't

Rename JProperty in json.net

巧了我就是萌 提交于 2019-12-29 01:46:25
问题 I've the following property { "bad": { "Login": "someLogin", "Street": "someStreet", "House": "1", "Flat": "0", "LastIndication": [ "230", "236" ], "CurrentIndication": [ "263", "273" ], "Photo": [ null, null ] } } and how can I rename this from 'bad' to 'good' for example. Yes, I saw the extension method by Abi Bellamkonda public static class NewtonsoftExtensions { public static void Rename(this JToken token, string newName) { var parent = token.Parent; if (parent == null) throw new

Selectively escape HTML in strings during deserialization

◇◆丶佛笑我妖孽 提交于 2019-12-29 01:41:13
问题 I'm looking to write a JsonConverter which escapes HTML in strings, unless the [AllowHtml] attribute has been applied; private class ObjectWithStrings { // will be HTML-escaped public string Name { get; set; } // won't be escaped [AllowHtml] public string Unsafe { get; set; } } So I'm trying to write a JsonConverter with a custom ReadJson property; public override bool CanConvert(Type objectType) { return objectType == typeof(string); } public override object ReadJson(JsonReader reader, Type

c# JSON Serialization Use Value Instead of Property Name

可紊 提交于 2019-12-29 00:44:28
问题 I am working on a JSON driven project and I would like to provide the SessionManager object with a dynamic list of permissionst. While I can work with an array of key value pairs for permissions, I was wondering if I could remove the property names so that the key is the Permission value and the value is the IsAllowed value. public class SessionPermission { public string Permission { get; set; } public bool IsAllowed { get; set; } } public class SessionManager { public string UserName { get;

c# JSON Serialization Use Value Instead of Property Name

北城以北 提交于 2019-12-29 00:44:26
问题 I am working on a JSON driven project and I would like to provide the SessionManager object with a dynamic list of permissionst. While I can work with an array of key value pairs for permissions, I was wondering if I could remove the property names so that the key is the Permission value and the value is the IsAllowed value. public class SessionPermission { public string Permission { get; set; } public bool IsAllowed { get; set; } } public class SessionManager { public string UserName { get;

Null-coalescing operator returning null for properties of dynamic objects

前提是你 提交于 2019-12-28 20:35:46
问题 I have recently found a problem with the null-coalescing operator while using Json.NET to parse JSON as dynamic objects. Suppose this is my dynamic object: string json = "{ \"phones\": { \"personal\": null }, \"birthday\": null }"; dynamic d = JsonConvert.DeserializeObject(json); If I try to use the ?? operator on one of the field of d, it returns null: string s = ""; s += (d.phones.personal ?? "default"); Console.WriteLine(s + " " + s.Length); //outputs 0 However, if I assign a the dynamic