json.net

Confusion in getting Parent from JToken

若如初见. 提交于 2019-12-05 03:43:30
I have the following JSON document stored in a text file { "attributes": {"attr0":"value0"}, "children" : { "ProductA" : { "attributes": {"attr1":"value1", "attr2":"value2"}, "children" : { "ProductC":{ "attributes": {"attr3":"value3", "attr4":"value4"}, "children" : {}, "referencedChildren" : {} } }, "referencedChildren" : {} }, "ProductB" : { "attributes": {"attr5":"value5", "attr6":"value6"}, "children" : {}, "referencedChildren" : {} } }, "referencedChildren" : {} } I have written this code in C# using NewtonSoft JSon.NET Library string content = File.ReadAllText(@"c:\temp\foo.txt");

Handling JSON single object and array

旧街凉风 提交于 2019-12-05 03:31:31
I'm using Newtonsoft.Json to work with some JSON data that is being returned to me. Depending on what I request I can either get back something that looks like: { "TotalRecords":2, "Result": [ { "Id":24379, "AccountName":"foo" }, { "Id":37209, "AccountName":"bar" } ], "ResponseCode":0, "Status":"OK", "Error":"None" } or { "Result": { "Id":24379, "AccountName":"foo" }, "ResponseCode":0, "Status":"OK", "Error":"None" } So sometimes "Result" is an array of Results or "Result" could be a single response. I've tried using the answer from How to handle both a single item and an array for the same

Newtonsoft.Json serialization of PagedList<T> is not including some properties

廉价感情. 提交于 2019-12-05 02:39:21
问题 I am trying to serialize a PagedList object ( https://github.com/martijnboland/MvcPaging/blob/master/src/MvcPaging/PagedList.cs ) to Json, like this: PagedList<Product> pagedList = new PagedList<Product>(products, (page - 1), pageSize); string json = Newtonsoft.Json.JsonConvert.SerializeObject(pagedList); If I use the above code, in the result I get an array of Product objects serialized properly. However the properties below (of PagedList) are not being included in the Json result: public

deserialize list of objects using json.net

人盡茶涼 提交于 2019-12-05 02:31:41
i have this class public class Image { public string url { get; set; } public string url_40px { get; set; } public string url_50px { get; set; } } public class Category { public List<int> ancestor_ids { get; set; } public int parent_id { get; set; } public List<object> children_ids { get; set; } public string nodename { get; set; } public int num_parts { get; set; } public List<Image> images { get; set; } public string __class__ { get; set; } public int id { get; set; } } and i deserialize it like this retObject = JsonConvert.DeserializeObject(Of Category)(jsonResp) but for a list of category

JSON.Net Struct Serialization Discrepancy

情到浓时终转凉″ 提交于 2019-12-05 02:27:55
When using JSON.Net to serialize/deserialize structs, a build-in struct type (like System.Drawing.Size) serializes to a string, whereas a custom struct type serializes to a JSON object. For example: using System; using System.Drawing; using Newtonsoft.Json; namespace TestJsonNet { class Program { static void Main(string[] args) { Console.WriteLine(JsonConvert.SerializeObject(new Size(50, 50))); Console.WriteLine(JsonConvert.SerializeObject(new Size2(50, 50))); } } struct Size2 { public int Width { get; set; } public int Height { get; set; } public Size2(int w, int h) : this() { Width = w;

Deserialize JSON as object or array with JSON.Net [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-05 02:21:14
This question already has an answer here: How to handle both a single item and an array for the same property using JSON.net 7 answers I want to know if it is possible to deserialize a JSON object that could either be an object or an array. Similar to this question: Jackson deserialize object or array But using JSON.Net. Example { "response": { "status":"success", // Could be either a single object or an array of objects. "data": { "prop":"value" } // OR "data": [ {"prop":"value"}, {"prop":"value"} ] } } You can change the property type for "data" in your model to dynamic or an object and

type or namespace name 'Newtonsoft' could not be found

半城伤御伤魂 提交于 2019-12-05 02:19:36
I've looked this question up, but I don't see too many answers, and obviously none have been helpful or I wouldn't be asking. I am a .NET neophyte. My local environment is Win7, Microsoft Virtual Web Developer 2010 Express. I added the NewtonSoft.Json as a custom component library. I built a simple "HelloWorld" web service, using Newtonsoft.Json custom components. When I do a build on my local machine (Visual Web Developer 2010 Express) it works great. I actually get a valid JSONP output, not XML. When I FTP my files to the remote web server, my web service does not work. I get this:

Serialize data to json string with dynamic property names

僤鯓⒐⒋嵵緔 提交于 2019-12-05 01:59:26
I have a method which accepts a key and a value. Both variables can have a dynamic content. key => is a dynamic string which can be everything like e.g. "LastSentDate" value => is an object which can be everything like e.g. "2014-10-10" As key is a dynamic value like "LastSentDate" or whatever key is passed to the method then I want that the json property is the value of the key string and not literally key itself... public void SetRowVariable(string key, object value) { var obj = new { key = value }; // key property is literally taken maybe anonym object is not a good idea? string jsonString

Provide ArrayPool object to JsonOutputFormatter constructor

梦想的初衷 提交于 2019-12-05 01:47:42
After upgrading from .net RC2 to RTM I find I need to supply a parameter to a constructor of JsonOutputFormatter that derives from ArrayPool. How do I get this object? I am newing JsonOutputFormatter manually because I need to configure ReferenceLoopHandling. Only other related info I could find is this: https://github.com/aspnet/Mvc/issues/4562 public IServiceProvider ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMemoryCache(); services.AddSession(); services.AddMvc(); var formatterSettings = JsonSerializerSettingsProvider.CreateSerializerSettings();

Set JSON CamelCase per Web API request

杀马特。学长 韩版系。学妹 提交于 2019-12-05 01:36:51
问题 Web API uses the Json.Net formatter to serialise its JSON responses which allows you to customise the format of the generated JSON very easily for the entire application at startup using: config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); This allows you resolve the issues between C# syntax preferring PascalCase and javascript based clients preferring camelCase. However setting this globally on the API without taking into