json.net

JSON.NET \ how to concat two JSONs in JSon.net

夙愿已清 提交于 2019-12-10 15:05:39
问题 I have two JSONs (as simple strings) - is there neat way to concat them? As part of the infrastructure?? 回答1: string j1 = @"{""a"":1}"; string j2 = @"{""b"":2}"; var j = JsonConvert.SerializeObject(new[] { JsonConvert.DeserializeObject(j1), JsonConvert.DeserializeObject(j2) }); or var j = JsonConvert.SerializeObject(new { obj1 = JObject.Parse(j1), obj2 = JObject.Parse(j2) }); 来源: https://stackoverflow.com/questions/12913396/json-net-how-to-concat-two-jsons-in-json-net

Json.NET serialization pre and post calls

左心房为你撑大大i 提交于 2019-12-10 14:52:08
问题 I am using Json.NET to serialize an object graph. For each object that is serialized or deserialized, I want to call a method on that object before the serialization takes place. For e.g. all my objects implement an interface INotified with a method OnSerializing. I want OnSerializing to be called before the object is serialized. Is there a way to achieve this without having to touch the Json.NET source code? 回答1: The latest version of Json.NET supports serialization callbacks. 来源: https:/

Why does this anonymous type not deserialize properly using JsonConvert.DeserializeAnonymousType?

帅比萌擦擦* 提交于 2019-12-10 14:50:36
问题 I have the JSON string: {"response":{"token":"{\"token\":\"123\",\"id\":191}"}} And then I have the following code to Deserialize it, but it is returning null: var def = new { token = new { token = string.Empty, id= string.Empty } }; var deserializedToken = JsonConvert.DeserializeAnonymousType(token, def); deserializedToken is null Here is a more detailed example that I can't get to work: var def = new { code = string.Empty, message = string.Empty, url= string.Empty, token = new {token =

How can I remove escape characters from my JSON object being displayed in Fiddler

妖精的绣舞 提交于 2019-12-10 14:34:31
问题 I would like to serialize my C# object to a JSON object without the final text string including escape characters. The below method is being called through a RESTful design and returns the following JSON object when called through fiddler, however I would like to remove the backslashes so it includes only the double quotes and respects the JSON format. "{\"model\":\"Faslev\",\"platform\":\"ABC\",\"year\":2010,\"month\":\"June\", \"plant\":\"ONDH\",\"country\":\"Brazil\"}" public string

Partially deserialize with JSON.NET, keeping some fields raw

流过昼夜 提交于 2019-12-10 14:34:03
问题 I have a document like this { "Field1": 1, "Field2": 2, "Field3": { Type: "TheMotherLoad" } } Which i want to convert into this class, but keeping field 3 "raw/as-is". public class Fields { public int Field1 { get; set; } public int Field2 { get; set; } public string Field3 { get; set; } } The result should be Field1 = 1, Field2 = 2, Field3 = "{ Type: "TheMotherLoad" }" Possible with Json.NET? 回答1: Field3 could be a JObject. When you need JSON just call Field3.ToString() 来源: https:/

JSON.NET Error reading JObject

只谈情不闲聊 提交于 2019-12-10 14:29:30
问题 I am sending a JSON object via AJAX and Web Api to my Server: var data = [ ["fdsfsd", "Kifdsfa", "fsdfsa", "fadsf", "fasdfsd", "fadsf", "fasdfsd"], ["2008", "-5", "11", "12", "13"], ["2009", "20", "-11", "14", "13"], ["2010", "30", "15", "-12", "readOnly"] ]; $.ajax({ url: '../webapi/Products', type: 'POST', dataType: "text", data: "="+JSON.stringify( data ), success: function (test) { alert(test); }, error: function (test) { alert("Error"); } so i am getting on server the value which i want

JSON.NET - Select All Objects

ε祈祈猫儿з 提交于 2019-12-10 14:16:33
问题 I am looking for a method to select ALL the objects in the JObject using Json.NET. So in short, if I had the following JSON: { "someCar" : { "id" : "3", "model" : "M7", "engine" : "FI V8", }, "name" : "carparkone", "id" : "1", "cars" : [ { "id" : "1", "model" : "s60", "engine" : "i5", }, { "id" : "2", "model" : "m3", "engine" : "FI V6", }, { "id" : "3", "model" : "M7", "engine" : "FI V8", } ] } I would run some command to get an array of all the objects in it, i.e. anything in {} blocks.

Deserializing an unnamed array

。_饼干妹妹 提交于 2019-12-10 14:10:29
问题 I am having difficulty writing the appropriate annotations to represent data which is returned from a JSON Get request which returns data like so: [{"ProductCode":"0129923083091","Description":"DIESEL ","SalesLitres":6058.7347,"SalesValue":6416.2000},{"ProductCode":"0134039344902","Description":"UNLEADED ","SalesLitres":3489.8111,"SalesValue":3695.7100}, ... ] (ellipsis above just indicating that I could have variable number of these items returned) In my model class (I am using MVVM approach

Why do I get a RuntimeBinderException using json.net with dynamic when calling a method

删除回忆录丶 提交于 2019-12-10 14:05:34
问题 Why is it that when I use dynamic with json.net I get a runtime binding exception then calling a method without casting but I can do assignments not problem private static void Main() { dynamic json = JObject.Parse("{\"Test\":23}"); var t = json.Test; int a = t; //Success Prop = t; //Success Func(t); //RuntimeBinderException } private static void Func(int i){} private static int Prop { get; set; } When I cast it to the correct type there are no errors but I would prefer to not have to do that

Exception creating JSON with LINQ

孤者浪人 提交于 2019-12-10 13:57:54
问题 I am trying to create JSON with the following code: JArray jInner = new JArray("document"); JProperty jTitle = new JProperty("title", category); JProperty jDescription = new JProperty("description", "this is the description"); JProperty jContent = new JProperty("content", content); jInner.Add(jTitle); jInner.Add(jDescription); jInner.Add(jContent); when I get to the jInner.Add(jTitle) , I get the following exception: System.ArgumentException: Can not add Newtonsoft.Json.Linq.JProperty to