json.net

How to handle error in Json.Net parsing

↘锁芯ラ 提交于 2019-12-07 07:55:04
问题 I'm using Json.Net for Json deserialization. On occasion the Json string I read is not correct (which I can't fix since I don't produce it). In particular, at one specific place, where there should be a string, sometimes there is a serialized object instead. Json.Net then complains, not surprisingly, about finding an object where it expected a string. I found I can intercept this by using Error in JsonSerializerSettings and also make Json.Net ignore the problem by setting ErrorContext.Handled

Serialize JSON object named “return”

我只是一个虾纸丫 提交于 2019-12-07 07:35:22
问题 I'm trying to write a ticker against Mt Gox's Http API. It returns JSON that looks like this: { "result":"success", "return": { "high": {"value":"5.70653","value_int":"570653","display":"$5.70653","currency":"USD"}, "low": {"value":"5.4145","value_int":"541450","display":"$5.41450","currency":"USD"}, "avg": {"value":"5.561119626","value_int":"556112","display":"$5.56112","currency":"USD"}, "vwap": {"value":"5.610480461","value_int":"561048","display":"$5.61048","currency":"USD"}, "vol": {

JSON.NET customizing the serialization to exclude a property name

回眸只為那壹抹淺笑 提交于 2019-12-07 06:59:28
I am using Json.NET and I have the following code. public class Row { [JsonProperty] public IDictionary<string, object> Cells { get; set; } } The rows and cells are dynamically generated and I had C# dynamic/Expando feature to create those Cells. When Json.NET serializes the dynamic instances, it produces the correct json structure I want. However for a large data structure it has an adverse effect on the performance. For example, JsonSerializer calls DynamicObject TryGetMember quite extensively during the serialization. Therefore I needed a static data structure so the serialization would be

Json.NET - How to serialize a class using custom resolver

旧时模样 提交于 2019-12-07 06:57:42
问题 I want to serialize this class: public class CarDisplay { public string Name { get; set; } public string Brand { get; set; } public string Year { get; set; } public PictureDisplay[] Pictures { get; set; } } public class PictureDisplay { public int Id { get; set; } public string SecretKey { get; set; } public string AltText { get; set; } } To this Json test: { Name: "Name value", Brand: "Brand value", Year: "Year value", Pictures: ["url1", "url2", "url3"] } Note that each Car have an pictures

Why can't use LINQ methods on JObject?

风流意气都作罢 提交于 2019-12-07 06:42:37
问题 Newtonsoft.Json.Linq.JObject implemented IEnumerable<T> , and not explicit implementation, but why can't do this: using System.Linq; ... var jobj = new JObject(); var xxx = jobj.Select(x => x); //error foreach(var x in jobj) { } //no error WHY? Thanks. 回答1: JObject implements both IEnumerable<KeyValuePair<string, JToken>> and IEnumerable<JToken> (by inheriting from JContainer ). Thus you cannot use LINQ (e.g. Select ) directly since it doesn't know which of the enumerables to 'extend'. Thus

How do I deserialize a JSON array that contains only values?

一个人想着一个人 提交于 2019-12-07 06:35:02
问题 I'm getting this result from a web function. ["767,20150221122715,121053103,14573465,1,7,302", "767,20150221122756,121053165,14573375,1,0,302", "767,20150221122840,121053498,14572841,1,12,124"] Usually Json have PropertyName: Value But this have an array of strings, and each string have the values separated by comma. I know what each value position mean. I try using JsonConvert.DeserializeObject but couldn't make it work. string deserializedProduct = JsonConvert.DeserializeObject<string>(json

Problems using JSON.NET with ExpandableObjectConverter

拜拜、爱过 提交于 2019-12-07 05:55:45
问题 I have the following class defined: <TypeConverter(GetType(ExpandableObjectConverter))> <DataContract()> Public Class Vector3 <DataMember()> Public Property X As Double <DataMember()> Public Property Y As Double <DataMember()> Public Property Z As Double Public Overrides Function ToString() As String Return String.Format("({0}, {1}, {2})", Format(X, "0.00"), Format(Y, "0.00"), Format(Z, "0.00")) End Function End Class Using the DataContractJsonSerializer I receive the following JSON as

Odata Controller: How to convert Odata response to C# object at client

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 04:46:49
问题 We have got a Odata response as below: "{\r\n \"@odata.context\":\"http://localhost/ApplicationService/model/$metadata#Edm.String\",\"value\":\"{\\\"Messages\\\":[\\\"message 1\\\",\\\"message 2\\\",\\\"message 3\\\",\\\"message 4\\\"],\\\"IsValidEntity\\\":false}\"\r\n}" Now say we have a class: public class myValidationResult { public myValidationResult() { Messages = new List<string>(); } public List<string> Messages { get; set; } public bool IsValidEntity { get; set; } } This class used

Avoiding CamelCasePropertyNamesContractResolver for a specific method

六眼飞鱼酱① 提交于 2019-12-07 04:26:27
问题 I have web api controllers and I am using this method of configuration in WebApiConfig file for camel casing my results for all controllers. var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); Now I have a controller's method which is giving data to Angularjs translation provider and all translation strings are not in camelcase in my html, thats why I need results of that method to be

Using Required and JsonRequired in ASP.NET Core Model Binding with JSON body

最后都变了- 提交于 2019-12-07 04:26:20
问题 I'm using ASP.NET Core 2.0, and I have a request object annotated like this: public class MyRequest { [Required] public Guid Id { get; set; } [Required] public DateTime EndDateTimeUtc { get; set; } [Required] public DateTime StartDateTimeUtc { get; set; } } And in my controller: public async Task<IActionResult> HandleRequest([FromBody] MyRequest request) { /* ... */ } I noticed an issue with model binding: When I send a request containing the header Content-Type set to application/json and a