json.net

Deserialisation of a nested JSON object with Newtonsoft in C# [closed]

自作多情 提交于 2019-12-08 08:45:44
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . Here is a sample JSON string that I'm trying to deserialize: { "background": "#ededf0", "theme": "Flat", "name": "Control Page Name", "scriptName": "Control Page Script", "objects": [{ "ID": "76799598", "position": { "top": 0.30428571428571427, "left": 0.6054421768707483 }, "width": 0

How to return json date from MVC4 controller in ISO format

谁说胖子不能爱 提交于 2019-12-08 08:32:13
问题 I tried tro return date in ISO format using Json.Net from ASP.NET MVC4 controller public JsonResult Sales() { var saleList = new List<Sale>(); ... var str = JsonConvert.SerializeObject(saleList); return Json(str, JsonRequestBehavior.AllowGet); } public class Sale { public DateTime saledate { get; set; } ... } But it returns whole object json notation as single string. How to return date in ISO format as json object ? 回答1: You can do it with ServiceStack JSON serializer but first you have to

Parse json property to a specific type

丶灬走出姿态 提交于 2019-12-08 08:19:12
问题 My starting point is something like this (simplified here) : private object GetPropValue(JToken token, Type type) { return JsonConvert.DeserializeObject(token["prop"].ToString(), type); } Usage : var value = GetPropValue(JObject.Parse(someJsonWithAPropertyNamedProp), typeof(someTypeFoundByReflection)); This works, except then the type is string. According to the doc, ToString() of a JValue should return a JSON, but when the JValue is a type string, the value returned is not a JSON, but rather

JSON to Dynamic Object vs. Strongly Typed Object

我与影子孤独终老i 提交于 2019-12-08 08:14:36
问题 I'm not sure if I don't get the big picture or if I just miss something but what are the benefits of parsing a JSON-String to a dynamic object? If I have a class like this class Product { public string Name { get; set; } public double Price { get; set; } public string Category { get; set; } } and I use the HttpClient to get the object like this Product product = await response.Content.ReadAsAsync<Product>(); What do I benefit from this code? string content = await response.Content

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

半腔热情 提交于 2019-12-08 07:25:11
问题 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. 回答1: 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

How to custom map property to JSON

对着背影说爱祢 提交于 2019-12-08 07:07:54
问题 Desired JSON: "MyRootAttr": { "value": "My Value" } Property definition in the class: public string MyRootAttr { get; set; } So, the question is, without defining a new class, with a single string property, how can i achieve the JSON I want? 回答1: If you don't want to alter your class structure, you can use a custom JsonConverter to achieve the result you want: class WrappedStringConverter : JsonConverter { public override bool CanConvert(Type objectType) { return (objectType == typeof(string)

JSON Serialisation - How to flatten nested object structures using JSON.net/Newtonsoft

我与影子孤独终老i 提交于 2019-12-08 06:38:39
问题 I have the following JSON structure : { "Name":"", "Children":[ { "ID":"1", "MetaData":[ { "Info":{ "GUID":"cdee360d-7ea9-477d-994f-12f492b9e1ed" }, "Data":{ "Text":"8" }, "Name":"DataId" } ] } ] } and I want to flatten the MetaData and nested Info and Data objects in the array. I also want to use the Name field as a field name for Text value so it becomes "DataId" : "8". { "Name":"", "Children":[ { "ID":"1", "GUID":"cdee360d-7ea9-477d-994f-12f492b9e1ed", "DataId":"8" } ] } So far I've used a

Adding new property to json object using VB.Net

两盒软妹~` 提交于 2019-12-08 06:27:56
问题 I'm trying to add a new property to an existing object from a json file. I would like to add a new profile being stored in a json file Here is the json structure of the file. You see, I need to go after: "profiles": { and add the stuff here, duplicate one of those profiles structure and paste it here with new data. ex. "NEW PROFILE NAME": {"name": "NEW PROFILE NAME", "lastVersionId": "VERSION ID" Here is what I have so far, but don't know what to do with it, maybe you will understand Imports

JSON.NET (WebApi2) - Serialize property, but skip during deserialization

╄→гoц情女王★ 提交于 2019-12-08 06:23:23
I know of [JsonIgnore] which ignores properties altogether, I know of ShouldSerializePropertyName which gives conditional serialization, but I cannot find anything to mark property to be serialized into JSON normally, but not set during deserialization. I could write a workaround: [JsonIgnore] public string MyValue{get;set;} public string MyValueForJson {get{return MyValue;} but it is a last resort. Is there some way - other than custom converters etc. - to express that I don't want that field to be populated during deserialization? As I understand you want to serialize object with all

How to create JSON arrays of “jagged” objects from a .NET data structure

Deadly 提交于 2019-12-08 05:44:20
问题 As a result of a LINQ-to-Entities projection, I end up with a List<ChartDataRecord> that looks like the following had I created it manually: List<ChartDataRecord> data = new List<ChartDataRecord>(); data.Add(new ChartDataRecord { date = 1370563200000, graph = "g0", value = 70 }); data.Add(new ChartDataRecord { date = 1370563200000, graph = "g1", value = 60 }); data.Add(new ChartDataRecord { date = 1370563200000, graph = "g2", value = 100 }); data.Add(new ChartDataRecord { date = 1370563260000