json.net

How to cause a JSON property to be converted to XML as an attribute of an xml-element

限于喜欢 提交于 2019-12-06 15:56:39
Using Newtonsoft's .Net Library to convert JSON to XML, is there a way to convert a specific JSON element to an XML attribute? For example, taking the following JSON: { "array": { "item": [ 1, 2, 3 ], "length": 3 } } and converting it to: <array length="3"> <item>1</item> <item>2</item> <item>3</item> </array> Thanks. Rob G Can you prefix the attributes with @ and put them at the top of the object? It says in the docs: Attributes are prefixed with an @ and should be at the start of the object. looks like: "@length": "3", for a definition of an attribute called 'length' Alternatively you could

Newtonsoft.Json, Path returned multiple tokens

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 15:30:39
For this code: JObject o = JObject.Parse(jsStr); var sel = o.SelectToken(".items[*].owner"); where the jsStr is the content of https://api.github.com/search/repositories?q=Newtonsoft.Json&sort=stars&order=desc I'll get the error of Path returned multiple tokens. How to make it works? The .SelectToken() method is for querying a single (string) value. You are getting an error because that path matches 60 values, not one. Instead, use .SelectTokens() , which returns an IEnumerable<JToken> : var vals = o.SelectTokens(".items[*].owner"); 来源: https://stackoverflow.com/questions/42303538/newtonsoft

C# Json.NET WCF Rest DateTime Format

六眼飞鱼酱① 提交于 2019-12-06 15:06:01
Take a look at the following example code, I would like the output to be a WCF date format "/Date(1237951967000)/" or the time zone variant. class Program { public class Test { public DateTime Date { get; set; } } static void Main(string[] args) { var test = new Test { Date = DateTime.Now }; var json = JsonConvert.SerializeObject(test); Console.WriteLine(json); } } Here is the output: {"Date":"2013-05-09T11:17:38.7990259-07:00"} How can I adjust the above code to give the desired format? {"Date":"\/Date(1237951967000)\/"} var settings = new JsonSerializerSettings() {DateFormatHandling=

dynamically deserialize json into any object passed in. c#

天大地大妈咪最大 提交于 2019-12-06 14:13:33
I'm trying to do is deserialize json into an object in c#. What I want to be able to do is pass any object get it's type and deserialize the json into that particular object using the JSON.Net library. Here are the lines of code. Object someObject1 = someObject; string result = await content.ReadAsStringAsync(); return JsonConvert.DeserializeObject<someObject1.GetType()>(result); The last line throws an exception of operator '<' cannot be applied to operands of type 'method group' How do I get the data type in the <> without c# complaining. What do I have to do to make this code work? And what

Efficient way of storing and retrieving Large Json ( 100 mb +) from a file using this article and Json.Net

与世无争的帅哥 提交于 2019-12-06 13:41:14
问题 I have been trying to figure out the best way to store and retrieve data from a file containing Json using JSon.net. During my research i found couple of ways. http://www.drdobbs.com/windows/parsing-big-records-with-jsonnet/240165316 http://www.ngdata.com/parsing-a-large-json-file-efficiently-and-easily/ Jackson Api ( only available for Java ) Can any of this method be used with Json.Net? Is there an actual implementation available for the article at DrDoobs? I could not figure out the

Exception parsing json with System.Text.Json.Serialization

笑着哭i 提交于 2019-12-06 13:23:56
My sample code is very simple: using System.Text.Json.Serialization; using Newtonsoft.Json; public class C { public C(string PracticeName) { this.PracticeName = PracticeName; } public string PracticeName; } var x = new C("1"); var json = JsonConvert.SerializeObject(x); // returns "{\"PracticeName\":\"1\"}" var x1 = JsonConvert.DeserializeObject<C>(json); // correctly builds a C var x2 = System.Text.Json.Serialization.JsonSerializer.Parse<C>(json); the last line raises: Exception thrown: 'System.NullReferenceException' in System.Text.Json.dll Object reference not set to an instance of an object

JSON.NET cannot deserialize a wrapped collection

扶醉桌前 提交于 2019-12-06 13:03:57
I have a wrapped list that looks like this: [Serializable] public class OrderManager : IEnumerable<Order> { public OrderManager() { } private List<Order> orders = new List<Order>(); public void AddOrder(Order order) { orders.Add(order); } public IEnumerator<Order> GetEnumerator() { return orders.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return orders.GetEnumerator(); } } I include a field instance in a customer class like this: [Serializable] public class Customer { public Customer() { } private OrderManager _orders = new OrderManager();

Pass javascript array to C# via json

痞子三分冷 提交于 2019-12-06 12:45:45
I´m using this javascript array: var exportOptions = [{ jobName: ""}, {exportType: ""}, {attachToEmail: ""}, {distributorName: ""}, {vistaNumber: ""}, {customerName: ""}, {competitors: ""}, {agreementType: ""}, { annualPotential: "" }, {businessCase: "" }]; And I passing to ASP.NET codebehind(C#) with this code: $.ajax({ type: 'POST', url: 'Epad.aspx/generateReport', data: "{'columnList': '" + columnList + "', 'exportOptions':" + JSON.stringify( exportOptions ) + "}", contentType: 'application/json; charset=utf-8', dataType: 'json', async: true, cache: false, }); And reading in C# with this

Reading dynamic attributes of json into .net C#

。_饼干妹妹 提交于 2019-12-06 12:23:46
I am getting following json format after hitting to an API: { "7407": { "survey_id": "406", "device_id": "1", "response_time": "2013-10-10 16:14:01", "timezone": "0", "language_id": "en", "response_id": "7407", "device_alias": "QR Code App", "site_name": "QR Code App", "country_name": "United States", "state_name": "New York", "city_name": "Suffern", "zip": "", "voucher_name": null, "voucher_mode": null, "surveyee_name": null, "surveyee_email": null, "surveyee_phone": null, "ques": { "": [] } }, "7408": { "survey_id": "406", "device_id": "1", "response_time": "2013-10-10 16:36:56", "timezone":

Visual studio 2012 project - Not able to load 'Newtonsoft.Json'

久未见 提交于 2019-12-06 12:09:04
问题 On running a windows phone project in VS 2012, I was getting this error: {System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. File name: 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' at MobileConnection.SSCClient.SSCConnection.GetHostNameAndPort() at MobileConnection.SSCClient