json.net

How to add JObject property by path if not exists?

懵懂的女人 提交于 2020-01-16 07:18:08
问题 Given the following code: var context = JObject.FromObject(new { data = new { mom = "", dad = "", sibling = "", cousin = "" } }); var path = "$.data.calculated"; var token = context.SelectToken(path); token will be null. Thus, of course, trying this will produce an exception: token.Replace("500"); I've seen other examples about how to add properties to a JObject , of course, but they all seem like you have to know something about the object structure beforehand. What I need to do is something

System.InvalidCastException while casting an object type to custom class

喜夏-厌秋 提交于 2020-01-15 12:06:48
问题 Today working on my code I came across a phase where i needed to cast an object type to my custom class which should have been very simple, but I ended up hours resolving System.InvalidCastException , finally I ended up using JsonConvert to get the job done. I am not really sure if there was a better way to handle this, if there is I would like to know what I could have done better. Given is the structure of my custom class using SQLite.Net.Attributes; namespace DataContract { public class

Powershell Code is working well on Powershell.exe and Powershell ISE but not working on VS Code

走远了吗. 提交于 2020-01-15 11:56:12
问题 I'm trying to validate Json against Schema using Powershell and Newtonsoft dlls. My script is working well as expected if I run it on Powershell.exe or Powershell ISE but it isn't working if I run it using VS Code (on same PC). $Json_file = "D:\Json\file_sample.json" $Json_file_wrong = "D:\Json\file_sample_wrong.json" $Json_Schema_file = "D:\Json\schema_sample.json" $Json = Get-Content $Json_file $Json_wrong = Get-Content $Json_file_wrong $SchemaJson = Get-Content $Json_Schema_file $Json_dll

Json deserialization fails

僤鯓⒐⒋嵵緔 提交于 2020-01-15 10:11:13
问题 I have following json string which I receive from API call : "\"{\\r\\n \\\"Table\\\": [\\r\\n {\\r\\n \\\"MaxDate\\\": \\\"2019-06-09T00:00:00\\\",\\r\\n \\\"MinDate\\\": \\\"2019-01-26T00:00:00\\\"\\r\\n }\\r\\n ]\\r\\n}\"" I want to deserialize this string to following class structure public class Dates { public DateTime MaxDate { get; set; } public DateTime MinDate { get; set; } } public class TableResult { public List<Dates> Table { get; set; } } When I try to deserialize this json

How to convert a JSON object containing an array to C# Dictionary<string,string>?

萝らか妹 提交于 2020-01-15 09:47:27
问题 I am sending a request to a WebAPI using following code: client.PostAsync(baseAddress + path, new FormUrlEncodedContent(JsonConvert.DeserializeObject<Dictionary<string,string>>(form))) where client is an object of HttpClient class. This code is executed for all the requests to the WebApi. I am trying to send following data to the API: { "code":"GUEST", "category":"Indian", "sections":["01000000-0000-0000-0000-000000000000","02000000-0000-0000-0000-000000000000"], "date":"0001-01-01T00:00:00",

Type metadata in JSON is apparently not honored if $type is not the first property in the object

我们两清 提交于 2020-01-15 08:39:29
问题 I went through the questions on the subject, and the closest to my situation didn't address my concern. I have the following classes : public abstract class BaseClass { } public class ConcreteClass { } My setting object for both the serialization and the deserialization is the following one : JsonSerializerSettings _serializationSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, TypeNameHandling = TypeNameHandling.All, ContractResolver = new

Building with .NET Native tool chain causes error with missing property in dynamic object

巧了我就是萌 提交于 2020-01-15 03:59:35
问题 I have a piece of code that gets a JSON response and checks whether there is a .error field dynamic jsonResponse = JsonConvert.DeserializeObject(responseString); if (jsonResponse.error != null) { error = jsonResponse.error; } else { success = true; } This runs successfully when it is not compiled with .NET Native toolchain but produces an error (on jsonResponse.error) when it's built with it. What is the reason for this? Any other similar incompatible behavior with native code? EDIT: It turns

JSON.NET: Deserialise and merge JSON string

与世无争的帅哥 提交于 2020-01-15 02:55:26
问题 I have a JSON string coming from a file with multiple JSON objects that I need to deserialise into one merged C# object. File1.json { "manage_employees_section_title": { "value": "Manage employees", "description": "The mange employees section title" } } { "manage_operations_section_title": { "value": "Manage operations", "description": "The mange operations section title" } } Even though there are multiple JSON objects in the file, I would really like to have back from deserialisation (or

JsonConvert.DeserializeObject, Index was outside the bounds of the array

六眼飞鱼酱① 提交于 2020-01-14 14:55:07
问题 All of this is origianlly from https://github.com/JamesNK/Newtonsoft.Json/issues/469 Posted here because I first looked on SO and didn't see anything, so I posted on the project's GitHub page. We're currently using JsonConvert.SerializeObject and JsonConvert.DeserializeObject<T> to send data between a client and server. I created a tool that creates 10 clients, sends a command to 10 different servers, the servers serialize a response and send it back over the network, then the 10 clients

Discarding garbage characters after json object with Json.Net

*爱你&永不变心* 提交于 2020-01-14 13:56:06
问题 I have to consume a so called web service implemented by a dumb monkey which is returning some garbage after the proper Json response. Something like this: { "Property1": 1, "Property2": 2, "Property3": 3 }<?xml version='1.0' ?>Maybe some other gibberish nonsense I wish to discard. Now, I could just search for "<?xml" and split, but I was wondering if I can use a stream reader or something to read up to the closing } and then discard the rest. I'm using C# and Json.Net. 回答1: You can also set