json.net

JSON.NET deserialize/serialize JsonProperty JsonObject

天涯浪子 提交于 2019-12-30 09:37:53
问题 I have a question for deserialize/serialize with the Newtonsoft JSON.NET library: I receive this json : { "customerName" : "Lorem", "id": 492426 "sequence": 1232132 "type" : 3, "status" : 0, "streetNumber" : 9675, "streetName" : "Lorem", "suite" : null, "city" : "IPSUM", "provinceCode" : "QC", "postalCode" : "H1P1Z3", "routeNumber" : 0, "poBox" : 0, "streetType" : "CH", "userId" : 25, "streetDirection" : null, "countryCode" : "CA", "customerNickName" : "Lorem ipsum", "streetSuffix" : null,

“EF BB BF” at the beginning of JSON files created in Visual Studio

…衆ロ難τιáo~ 提交于 2019-12-30 08:25:33
问题 I have a bunch of JSON files set as Embedded resource in one of my projects. I'm using Newtonsoft.Json to parse these files: public static string ReadStringFromStream(string streamName) { using (System.IO.Stream stream = new EmbeddedResourceReader().GetType().Assembly.GetManifestResourceStream(streamName)) { byte[] result = new byte[stream.Length]; stream.Read(result, 0, (int)stream.Length); var str = Encoding.UTF8.GetString(result); return str; } } ... var traits = JsonConvert

Add JObject to JObject

◇◆丶佛笑我妖孽 提交于 2019-12-30 06:00:37
问题 I have a json structure like this: var json = { "report": {}, "expense": {}, "invoices": {}, "projects": {}, "clients": {}, "settings": { "users": {}, "companies": {}, "templates": {}, "translations": {}, "license": {}, "backups": {}, } } I would like to add a new empty Object like "report":{} to the json My C# code is like this: JObject json = JObject.Parse(File.ReadAllText("path")); json.Add(new JObject(fm.Name)); But it it gives me a exception: Can not add Newtonsoft.Json.Linq.JValue to

Add JObject to JObject

时间秒杀一切 提交于 2019-12-30 06:00:10
问题 I have a json structure like this: var json = { "report": {}, "expense": {}, "invoices": {}, "projects": {}, "clients": {}, "settings": { "users": {}, "companies": {}, "templates": {}, "translations": {}, "license": {}, "backups": {}, } } I would like to add a new empty Object like "report":{} to the json My C# code is like this: JObject json = JObject.Parse(File.ReadAllText("path")); json.Add(new JObject(fm.Name)); But it it gives me a exception: Can not add Newtonsoft.Json.Linq.JValue to

Json.NET StringEnumConverter not working as expected

雨燕双飞 提交于 2019-12-30 05:46:06
问题 I'm attempting to use Json.NET with the System.Net.Http.HttpClient to send an object with an enum property, however the enum is always serialized as an integer value rather than the string equivalent. I've tried following the instructions here: http://james.newtonking.com/archive/2013/05/08/json-net-5-0-release-5-defaultsettings-and-extension-data By both adding an instance of StringEnumConverter to the JsonSerializerSettings and also tried to decorate the enum property with [JsonProperty

Json.NET, how to customize serialization to insert a JSON property

。_饼干妹妹 提交于 2019-12-30 05:39:05
问题 I have been unable to find a reasonable implementation for JsonConvert.WriteJson that allows me to insert a JSON property when serializing specific types. All my attempts have resulted in "JsonSerializationException : Self referencing loop detected with type XXX". A little more background on the problem I'm trying to solve: I am using JSON as a config file format, and I'm using a JsonConverter to control the type resolution, serialization, and deserialization of my configuration types.

Deserializing complex object using Json.NET

落爺英雄遲暮 提交于 2019-12-30 04:21:32
问题 I need to deserialize the this json returned from grogle maps api: { "destination_addresses": [ "Via Medaglie D'Oro, 10, 47121 Forlì FC, Italia", "Via Torino, 20123 Milano, Italia", "Via Guglielmo Marconi, 71, 40121 Bologna, Italia", "Via Irnerio, 40126 Bologna, Italia" ], "origin_addresses": [ "Via Medaglie D'Oro, 10, 47121 Forlì FC, Italia", "Via Torino, 20123 Milano, Italia", "Via Guglielmo Marconi, 71, 40121 Bologna, Italia", "Via Irnerio, 40126 Bologna, Italia" ], "rows": [ { "elements":

How to serialize ANY object into a string?

旧街凉风 提交于 2019-12-30 03:19:23
问题 I'm running into an issue where my JSON serializer is failing randomly due to the character < showing up from time to time. I can't nail down where this is coming from and I want to - on exception - reserialize using a different method so I can see a full representation of the offending object. Is there any way to do this? My current code: // data is of type 'object' serialized = JsonConvert.SerializeObject(data, new JsonSerializerSettings() { Error = delegate(object sender, ErrorEventArgs

Is there a way to use Json.Net deserialization with immutable classes?

℡╲_俬逩灬. 提交于 2019-12-30 01:36:13
问题 I'm working with an API that uses json. I have some classes that I've created to model the API. To make life easy, my models use public properties, which are in turn used by Json.Net when deserializing the json into objects. I'd like to make my objects immutable, but I'm running into a problem because if I make my properties read only, I break the deserialization. Is there a way for me to have immutable objects, and use deserialization? 回答1: I think you should be able to use

JSON.NET DeserializeObject to List of Objects

巧了我就是萌 提交于 2019-12-30 01:35:42
问题 I'm trying to Deserialize object to list of object using JSON.NET lib. My json file is: [ { "id": 1, "name": "Poczta", "description": "Opis", "latitude": 52.25197, "longitude": 20.896355, "accuracy": 0, "type": "", "image": null }, { "id": 2, "name": "WAT", "description": "Budynek główny - sztab.\r\nzażółć gęślą jaźń", "latitude": 52.2531213, "longitude": 20.8995849, "accuracy": 0, "type": "Uczelnia", "image": null }, { "id": 3, "name": "Przychodnia", "description": "Opis", "latitude": 52