json.net

Post json object to ASP MVC doesn't work properly

雨燕双飞 提交于 2019-12-23 06:00:54
问题 I'm trying to post a custom Json object to ASP.NET MVC controller, but doesn't work properly. My JS Code is: var myData = { "Message": "Another message", "Value": 4, "FirstItem": { "ItemName": "Este es el primer Item" }, "SecondItem": { "ItemName": "Este es el segundo Item" } }; $('.clickeable').click(function () { $.ajax({ type: 'POST', dataType: 'json', data: myData, url: '@Url.Action("PostSomething")', success: function () { console.info('Success'); }, error: function () { console.info(

How to move JSON object's depth one level up in JSON.net using C#?

走远了吗. 提交于 2019-12-23 05:25:15
问题 I am trying to write a converter for a class using JSON.net When I serialize the object of type JsonObject , I get the following output {"DataObject":{"id":"1","name":"data name"}} How can I move the DataObject one level up to get the following output: {"id":"1","name":"data name"} You can find the relevant code below. My class has the following format: public class JsonObject { public JsonObject(IDataObject dataObject) { this.DataObject= dataObject; } [JsonConverter(typeof

NullValueHandling setting for a class on JSON.NET by JsonConverter attribute (for Azure DocumentDb)

狂风中的少年 提交于 2019-12-23 05:15:16
问题 I have several DAO files, which are stored in Azure DocumentDb, and now I want that the null values won't be stored in the DocDb, it is possible by the [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] attribute for properties. But I don't wan't to put such attribute on every property. The problem is, there isn't any way to set the JsonSerializerSettings for the Json serializer used by the Azure DocumentDb API. The way which seems for me to go, is to use the JsonConverter attribute

Json nested parameters

吃可爱长大的小学妹 提交于 2019-12-23 03:49:06
问题 I am using data from a Json object to populate a list view. The object has these parameters: "id": "339150749455906", "posts": { "data": [ { "id": "339150749455906_545370565500589", "from": { "category": "Food/beverages", "name": "Pepsi", "id": "339150749455906" }, "story": "Pepsi updated their cover photo.", "picture": "http://photos-g.ak.fbcdn.net/hphotos-ak-ash3/942740_545370555500590_46289134_s.jpg", "link": "http://www.facebook.com/photo.php?fbid=545370555500590&set=a.365573920146922

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

Deadly 提交于 2019-12-23 03:32:24
问题 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

Is there a pull-style JSON StreamReader parser for .NET?

痞子三分冷 提交于 2019-12-23 03:13:49
问题 I need to deserialize from a JSON StreamReader , without instantiating intermediate objects for every object in that stream. JSON.NET invoked like JsonConvert.DeserializeObject<T>(string json, JsonConverter[] converters) doesn't accept StreamReader , and creates in memory the whole set of Newtonsoft.Json.* classes for the JSON structore, as memory profiler shows. _I'm not actually sure this is the case, because my JsonConverter s _ JavaScriptSerializer also doesn't seem to support Stream s at

Newtonsoft escaped JSON string unable to deseralize to an object

浪子不回头ぞ 提交于 2019-12-23 02:03:04
问题 Question background: I am receiving a JSON response via a HttpResponseMessage, as shown: var jsonString= response.Content.ReadAsStringAsync().Result; This is giving me the following simple escaped JSON string result: "\"{\\\"A\\\":\\\"B\\\"}\"" The problem: I am using Newtonsoft to try and deserialize this to a model: SimpleModel simpleModel= JsonConvert.DeserializeObject<SimpleModel>(jsonString); The Class model of SimpleModel : public class SimpleModel { public string A { set; get; } } The

JSON.NET customizing the serialization to exclude a property name

自闭症网瘾萝莉.ら 提交于 2019-12-23 01:19:51
问题 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

Serialize hashtable using Json.Net

天大地大妈咪最大 提交于 2019-12-23 01:12:41
问题 I have a hashtable whose keys are of type integer, however when deserializing using json.net the keys come back as strings, is there a way to keep the key type on hashtable using json.net serialization/deserialization? This hashtable is a property of the type 'MyType' var settings = new JsonSerializerSettings(); settings.TypeNameHandling = TypeNameHandling.Objects; string json = JsonConvert.SerializeObject(o, Formatting.Indented, settings); mo = JsonConvert.DeserializeObject<MyType>(json, new

Using Linq to select into an object with children from Json

若如初见. 提交于 2019-12-22 19:24:31
问题 I'm a novice C# dev and newer to Linq, but I'm trying to figure out how to get the nested child objects from a Linq query into an object. I can get the main/root objects. This is my code so far and the Json file I'm using. Note, I manually removed some text from the Json if it happens to appear malformed. class Program { static void Main(string[] args) { Program p = new Program(); p.processJson(); Console.ReadLine(); } public void processJson() { JObject jobject = JObject.Parse(this