json.net

How to apply indenting serialization only to some properties?

妖精的绣舞 提交于 2019-12-17 16:35:20
问题 I want to serialize .NET objects to JSON in a human-readable way, but I would like to have more control about whether an object's properties or array's elements end up on a line of their own. Currently I'm using JSON.NET's JsonConvert.SerializeObject(object, Formatting, JsonSerializerSettings) method for serialization, but it seems I can only apply the Formatting.Indented (all elements on individual lines) or Formatting.None (everything on a single line without any whitespace) formatting

How to get the name of <T> from generic type and pass it into JsonProperty()?

喜你入骨 提交于 2019-12-17 16:34:36
问题 I get the following error with the code below: "An object reference is required for the non-static field, method, or property 'Response.PropName'" Code: public class Response<T> : Response { private string PropName { get { return typeof(T).Name; } } [JsonProperty(PropName)] public T Data { get; set; } } 回答1: What you're trying to do is possible, but not trivial, and can't be done with only the built-in attributes from JSON.NET. You'll need a custom attribute, and a custom contract resolver.

dynamic JContainer (JSON.NET) & Iterate over properties at runtime

大城市里の小女人 提交于 2019-12-17 16:33:50
问题 I'm receiving a JSON string in a MVC4/.NET4 WebApi controller action. The action's parameter is dynamic because I don't know anything on the receiving end about the JSON object I'm receiving. public dynamic Post(dynamic myobject) The JSON is automatically parsed and the resulting dynamic object is a Newtonsoft.Json.Linq.JContainer . I can, as expected, evaluate properties at runtime, so if the JSON contained something like myobject.myproperty then I can now take the dynamic object received

Parsing ISO Duration with JSON.Net

核能气质少年 提交于 2019-12-17 16:26:42
问题 I have a Web API project with the following settings in Global.asax.cs : var serializerSettings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, DateTimeZoneHandling = DateTimeZoneHandling.Utc }; serializerSettings.Converters.Add(new IsoDateTimeConverter()); var jsonFormatter = new JsonMediaTypeFormatter { SerializerSettings = serializerSettings }; jsonFormatter.MediaTypeMappings.Add(GlobalConfiguration.Configuration.Formatters[0].MediaTypeMappings[0]);

ASP.NET Web API Date format in JSON does not serialise successfully

旧巷老猫 提交于 2019-12-17 16:26:15
问题 All, We are using ASP.NET Web API where we have a REST based service with JSON for the payload. If I pass the following Date as a string e.g sampleObj: { ... myDate: "31/12/2011 00:00:00", ... } as an attribute value in the JSON payload, the date attribute gets deserialised into a DateTime.MinValue. Is the string format valid? We know the format "2012-10-17 07:45:00" serialises successfully but we cannot guarantee that all dates received will be in this format. What are the valid options? 回答1

Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path

随声附和 提交于 2019-12-17 16:24:04
问题 I am working on a Windows Phone 8.1 application involving location. I am receiving Json data from my API. My API returns data that looks like: [{ "country": "India", "city": "Mall Road, Gurgaon", "area": "Haryana", "PLZ": "122002", "street": "", "house_no": "", "POI": "", "type": "17", "phone": "", "lng": 77.08972334861755, "lat": 28.47930118040612, "formatted_address": "Mall Road, Gurgaon 122002, Haryana, India" }, { "country": "India", "city": "Mall Road, Kanpur", "area": "Uttar Pradesh",

Json.Net: JsonSerializer-Attribute for custom naming [duplicate]

雨燕双飞 提交于 2019-12-17 16:15:34
问题 This question already has answers here : How can I change property names when serializing with Json.net? (3 answers) Closed 3 years ago . I use the JsonSerializer from Newtonsoft. But i want to name the json-objects by myself. I tried the JsonObject attribute [JsonObject(Description = "MyName", Title = "orThisname")] JsonArray also didnt work... Is it possible to name the json-objetcs/arrays by myself? 回答1: I think I found the solution: [JsonProperty(PropertyName = "Myname")] will rename it.

Generate JSON object with NewtonSoft in a single line

梦想的初衷 提交于 2019-12-17 15:34:56
问题 I'm using the JSON library NewtonSoft to generate a JSON string: JObject out = JObject.FromObject(new { typ = "photos" }); return out.ToString(); Output: { "typ": "photos" } My question: Is it possible to get the output in a single line like: {"typ": "photos"} 回答1: You can use the overload of JObject.ToString() which takes Formatting as parameter: JObject obj = JObject.FromObject(new { typ = "photos" }); return obj.ToString(Formatting.None); 回答2: var json = JsonConvert.SerializeObject(new {

How do you add a JToken to an JObject?

陌路散爱 提交于 2019-12-17 15:26:09
问题 I'm trying to add a JSON object from some text to an existing JSON file using JSON.Net. For example if I have the JSON data as below: { "food": { "fruit": { "apple": { "colour": "red", "size": "small" }, "orange": { "colour": "orange", "size": "large" } } } } I've been trying to do this like this: var foodJsonObj = JObject.Parse(jsonText); var bananaJson = JObject.Parse(@"{ ""banana"" : { ""colour"": ""yellow"", ""size"": ""medium""}}"); var bananaToken = bananaJson as JToken; foodJsonObj[

JObject.Parse vs JsonConvert.DeserializeObject

≡放荡痞女 提交于 2019-12-17 15:24:46
问题 What's the difference between JsonConvert.DeserializeObject and JObject.Parse? As far as I can tell, both take a string and are in the Json.NET library. What kind of situation would make one more convenient than the other, or is it mainly just preference? For reference, here's an example of me using both to do exactly the same thing - parse a Json string and return a list of one of the Json attributes. public ActionResult ReadJson() { string countiesJson = "{'Everything':[{'county_name':null,