json.net

Generic Type JsonConvert.DeserializeObject<List<T>>(string)

你说的曾经没有我的故事 提交于 2019-12-07 02:11:53
问题 I am using Newtonsoft.JSON. I won't know the type of object passed to this method, or retrieved to this method, so I am attempting to use DeserializeObject on an object I do not know the type of. Is this possible? If so, how? Here is my code. public static List<T> GetObject<T>(string cacheKey, IEnumerable<T> obj) { using (HttpClient client = new HttpClient()) { var response = client.GetAsync("http://localhost:53805/api/NonPersisted/Get/" + cacheKey).Result; obj = JsonConvert.DeserializeObject

C# serialize and deserialize json to txt file

不打扰是莪最后的温柔 提交于 2019-12-07 01:52:24
问题 I'm using NewtonSoft for handling json in my wpf application. I've got a customer that can be saved to a txt file (no database involved). I'm doing that like this: public int store(string[] reservation) { JObject customer = new JObject( new JProperty("id", this.getNewId()), new JProperty("name", reservation[0]), new JProperty("address", reservation[1]), new JProperty("gender", reservation[2]), new JProperty("age", reservation[3]) ); using (StreamWriter file = File.CreateText(Settings

MVC WebApi HttpGet with complex object

谁都会走 提交于 2019-12-07 01:25:02
问题 I have an existing WebApi action, that I want to switch from HttpPost to HttpGet. It currently takes a single complex object as parameter. The model: public class BarRequest { [JsonProperty("catid")] public int CategoryId { get; set; } } The controller: public class FooController : ApiController { //[HttpPost] [HttpGet] [ActionName("bar")] public void Bar([FromUri] BarRequest request) { if (request != null) { // CategoryId should be 123, not 0 Debug.WriteLine("Category ID :: {0}", request

Pass Base64 encoded string using JsonTextReader value as new stream

爱⌒轻易说出口 提交于 2019-12-07 00:51:47
We are consuming large JSON streams from an HTTP Post request. The goal is to stream the incoming body as JSON using JsonTextReader and extract the embedded base64 encoded binary files to disk. In XML, an equivalent method might be XMLReader.ReadElementContentAsBase64Async. Using JSON.NET, as we iterative how do we send the each item of the encodedImages array into a FileStream without holding the whole string in memory. Example JSON Object: { "company":"{clientCompany}", "batchName":"{clientBatchName}", "fileType":"{clientFileType}", "encodedImages":[ "{base64encodedimage}", "

JSon.NET DeserializeObject<List<T>> not returning list nor giving error

假装没事ソ 提交于 2019-12-07 00:30:16
问题 I got stuck with Json.NET library and its DeserializeObject method. The Documentation is not quite clear with what could be happening here, so I would appreciate if somebody could explain how to acheive to deserialize JSON into list of User objects. I'm trying to deserialize this JSON [ {"userid":"0", "listid":1, "lastname":"Mann", "inplace":true, "xpos":428, "ypos":111 }, {"userid":"1", "listid":1, "lastname":"Parker", "inplace":true, "xpos":334, "ypos":154 }, {"userid":"2", "listid":1,

How to log ASP.NET Core input JSON serialization errors

耗尽温柔 提交于 2019-12-06 21:16:45
问题 We have ASP.NET Core application which we use as a REST api. Methods use [FromBody]MyClass param as input. Problem is if the client sends an invalid JSON string the input value becomes null due to JSON serialization errors. Is there a way to log such errors? Edit: I'm looking for application-wide solution not per-method.. 回答1: I've solved this with adding an error handler in Startup.cs: services.AddMvc() .AddJsonOptions(options => { options.SerializerSettings.Error = (object sender,

JSON decoding: Unexpected token: StartArray

雨燕双飞 提交于 2019-12-06 20:33:31
问题 I'm using JSON.net to decode a JSON string and I find this error: Exception in 'Newtonsoft.Json.JsonReaderException' en Newtonsoft.Json.dll Información adicional: Error reading string. Unexpected token: StartArray. Path 'mentions', line 3, position 3. The JSON string is something like this: { "mentions": [ { "id":"1234", "alert_id":123, "title":"Bla bla bla", "url":"http:\/\/www.example.com\/", "unique_id":"123", "published_at":"2013-07-30T11:26:36.92131100+00:00", "created_at":"2013-07-30T11

Which .NET JSON serializers can deal with NHibernate proxy objects?

白昼怎懂夜的黑 提交于 2019-12-06 19:51:30
Which .NET JSON serializers can deal with NHibernate proxy objects? I tried using JSON.NET but it craps out when it hits a proxied object. Jay I would say that the best idea would be to deal with the proxy objects rather than actually find another way to serialise JSON. I have answered another question which involves eager loading these proxy objects in a legacy database which returns either the correct object or null here . Please also note that with the proper database set up with foreign keys and definite links between objects/tables you shouldn't run into any proxy objects. Also note that

Copy JsonSerializerSettings from JsonSerializer to new JsonSerializer

人盡茶涼 提交于 2019-12-06 19:14:25
问题 Is there any way to take the settings out of a JsonSerializer class and reimplement them in a new JsonSerializer ? There doesn't seem to be any methods to do anything like that. The best I found was a private method to be called through reflection, ApplySerializerSettings . I'm trying to take the serializer from the WriteJson method and copy it into a new serializer, tweaking it a bit. Specifically I want to replace the ContractResolver . public override void WriteJson(JsonWriter writer,

Newtonsoft.Json causing serialization to happen twice causing duplicate definition in the Reference.cs

可紊 提交于 2019-12-06 19:03:07
问题 I have a project Common that has a service reference. After adding a reference to Newtonsoft.json(Version 6.0.2 to the same project(Common) which has service reference, and a Serializable class ChatLine [Serializable] public class ChatLine { [JsonProperty("L")] public string LineId { get; set; } [JsonProperty("CT")] public DateTime ConversationTimeInUtc { get; set; } [JsonProperty("S")] public string SenderId { get; set; } [JsonProperty("R")] public IEnumerable<string> Recipients { get; set;