json.net

C# remove json child node using newtonsoft

三世轮回 提交于 2020-01-24 04:17:26
问题 I am developing an app using c# wpf in .net 3.5. I use newtonsoft library to parse the json string. I want to know how to remove a child node of json. For example, My json data = {"employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"}]} The function jobject.Remove("employees"); removes all the nodes sucessfully I would like to know how to remove the first employee detail alone. 回答1: Once you parse your json

Newtonsoft.Json, Path returned multiple tokens

社会主义新天地 提交于 2020-01-23 13:22:28
问题 For this code: JObject o = JObject.Parse(jsStr); var sel = o.SelectToken(".items[*].owner"); where the jsStr is the content of https://api.github.com/search/repositories?q=Newtonsoft.Json&sort=stars&order=desc I'll get the error of Path returned multiple tokens. How to make it works? 回答1: The .SelectToken() method is for querying a single (string) value. You are getting an error because that path matches 60 values, not one. Instead, use .SelectTokens() , which returns an IEnumerable<JToken> :

How do I use Json.NET to parse json in PowerShell?

为君一笑 提交于 2020-01-22 19:48:10
问题 I want to parse JSON in PowerShell but I can't use the new v3 functions that are available in PowerShell. My first thought was to load the JSON.Net assembly and use that to parse the JSON string but it doesn't work as I expect it to. I have this JSON: $json = "{""Name"": ""Apple"", ""Price"": 3.99, ""Sizes"": [ ""Small"", ""Medium"", ""Large""]}" I load the JSON.NET assembly with this code: [Reflection.Assembly]::LoadFile("$currentPath\Newtonsoft.Json.dll”) And tries to parse it with $result

Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'

大城市里の小女人 提交于 2020-01-22 17:30:12
问题 I am testing my Web API. Mocking the data I have this: var objs = ((JArray)JsonConvert.DeserializeObject("{ \"PrintId\":10,\"Header\":\"header\",\"TC\":\"tc\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}")).Values<JObject>(); Which gives me the error: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray' The thing is it was working. I must have changed something, but I don't know what. My intent is to convert this JSON object to a list

Parsing Multidimensional JSON Array with Newtonsoft Json.NET

孤街浪徒 提交于 2020-01-22 10:04:11
问题 I have Json returning as the following: [{"CreatedBy":"GIS_DB","CreatedDate":"3/8/2012 10:44:00 AM","Id":39,"ModifiedBy":"","ModifiedDate":"","Name":"CF-39","StatusId":1,"TrailCoordinates":[{"CreatedBy":"GIS_DB","CreatedDate":"3/8/2012 10:44:00 AM","Id":1637,"Latitude":32.76004207,"Longitude":-97.34006853,"ModifiedBy":"","ModifiedDate":"","SortOrder":1,"TrailId":39},{"CreatedBy":"GIS_DB","CreatedDate":"3/8/2012 10:44:00 AM","Id":1638,"Latitude":32.76004333,"Longitude":-97.34012121,"ModifiedBy

Deserialize JSON to subclasses

别来无恙 提交于 2020-01-21 11:27:09
问题 I have 3 classes: class Person { public bool IsFemale { get; set; } } class Female : Person { public string FemaleSpecificProperty { get; set; } } class Male: Person { public string MaleSpecificProperty { get; set; } } How can I deserialize given JSON string into specific instances ( Female or Male ) based on the value of Person.IsFemale property? (upfront I dont know which exact type has been used and that property is the only indication) I looked at employing CustomCreationConverter<T> but

Deserialize a property as an ExpandoObject using JSON.NET

被刻印的时光 ゝ 提交于 2020-01-21 04:06:32
问题 For example, there's an object like the next one: public class Container { public object Data { get; set; } } And it's used this way: Container container = new Container { Data = new Dictionary<string, object> { { "Text", "Hello world" } } }; If I deserialize a JSON string obtained from serializing the above instance, the Data property, even if I provide the ExpandoObjectConverter , it's not deserialized as an ExpandoObject : Container container = JsonConvert.Deserialize<Container>(jsonText,

How can I Dump() a Newtonsoft JObject in LinqPad?

↘锁芯ラ 提交于 2020-01-21 03:58:24
问题 In LinqPad, trying to call .Dump() on a Newtonsoft JSON.Net JObject yields an exception: RuntimeBinderException: 'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'Dump'. This works for almost everything else in LinqPad. I'd like to figure out a method that will Dump out a Newtonsoft JObject , just like other objects, showing property names, values, etc. I've already figured out how to get it to dump the JSON string, but I'd like to see an object get output rather than just a

Convert a Treeview to JSON using C#

大城市里の小女人 提交于 2020-01-20 07:21:25
问题 I have to convert a TreeView object to JSON using C#. I am currently using JsonConvert.SerializeObject() . public class SubTreeNode : TreeNode { public CustomProperties customProperties; } public class CustomProperties { public string property1 = "Property1"; public string property2 = "Property2"; public string property3 = "Property3"; } When tried it with JsonConvert.SerializeObject(treeView1.Nodes); it only returned the top nodes... not the child nodes, sub-child, and so on. What is the

Json.net DefaultValueHandling exempting boolean alone

左心房为你撑大大i 提交于 2020-01-19 16:25:09
问题 While serializing using json.net i used DefaultValueHandling.Ignore in serialization settings, which result in removal of key if the bool is set false. I want that to be exempted for type bool alone, and apply for other types and classes. Please help Thanks in advance. 回答1: DefaultValueHandling.Ignore in serialization settings can be overridden by decorating any property with [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)] attribute. Here is the class: public class Person