json.net

Issue with JSON null handling in Newtonsoft

微笑、不失礼 提交于 2019-12-11 02:44:15
问题 I have an issue with null handling when dealing Newtonsoft.json . I want to check the result is null or not. Based on that I want to handle some condition. My code is as below: try { var response = GetApiData.Post(_getApiBaseUrl, data.ToString()); var jsonString = response.ResultString; var jsonContent = JObject.Parse(jsonString); if (jsonContent["User"] != null) // <-- null handling { var user = JToken.Parse(jsonContent["User"].ToString()); membershipUser = GetMembershipUser(user); } } The

Parsing in Json not in XML . VB.Net

霸气de小男生 提交于 2019-12-11 02:34:24
问题 At the moment I am using VB.Net. I build my string, post it out and then parse the results. Parsing Example for XML Dim xml As New MWXMLDocument() Dim sReason As String = "Unknown" Try xml.LoadXml(sresult) If xml.SelectSimpleNode("AcceptedLead").InnerText = "true" Then app.Outcome.RedirectURL = xml.SelectSimpleNode("result/redirecturl").InnerText AcceptLead() Return True End If sReason = xml.SelectSimpleNode("Reason").InnerText Catch ex As Exception sReason = "Error: " & ex.Message End Try

Json.NET decimal precision loss

﹥>﹥吖頭↗ 提交于 2019-12-11 02:34:17
问题 I have an issue with deserializing decimal value. JObject.Parse("{\"available\":8777.831438322572000}") If I type this code in VS under debugger the result is "available": 8777.8314383225716 If I try this obj.Value<decimal>("available") the result is 8777.83143832257 Where am I wrong? What api methods should I use to get correct results? 回答1: The result of JObject.Parse("{\"available\":8777.831438322572000}") is a double . The second statement results in a decimal . The double has floating

accessing items in an json.net jarray in c#

偶尔善良 提交于 2019-12-11 02:31:39
问题 My api returns { "result": [ { "id": "51473", "name": "serv-vc", "modifydate": "2014-10-09 18:29:48.033", "expirationoff": "false", "createdate": "", "scheduleoff": "false", } ], "status": 0 } which i've stored as a JObject reponseobj I'm having trouble figuring out how to access responseobj["result"][0]["id"] every time i try that it gives an array about being out of bounds. What am i missing? I also tried JArray resultarr = (JArray)responseobj.SelectToken("result"); resultarr[0]["id"] but

deserialize json into .net object using json.net

百般思念 提交于 2019-12-11 02:19:32
问题 I am having a problem deserializing some JSON string back into .net objects. I have a container class which contains some information from external and there is a field call ClassType which defined what type of information is that and the actual content is in another property, which currently can be anything, so we define that as an Object type. Following are the .net class definition which helps to understand the issue. class ClassOne { public string Name { get; set; } public int Age { get;

Mapping multiple property names to the same field in Newtonsoft.JSON

半世苍凉 提交于 2019-12-11 02:12:34
问题 I have two components in a distributed system, which send messages which are serialized/deserialized using Newtonsoft.JSON (JSON.Net). The message properties are currently sent in Norwegian, and I am looking to translate the codebase to English. Since there is a change that some messages would have been sent in Norwegian, and handled by a component which has been upgraded to the English version, it needs to be able to support both. I would like that on deserialization, both the 'Norwegian'

Json.NET implicit casting

怎甘沉沦 提交于 2019-12-11 01:58:22
问题 I have this code : class Sometype { public Number pos { get; set; } } class Number { private uint num; private Number(uint num) { this.num = num; } public static implicit operator uint(Number sid) { return sid.num; } public static implicit operator Number(uint id) { return new Number(id); } } And this json : {"pos":123} When I try to deserialize this, I get a JsonException Error converting value 123 to type 'Tester2.Program+Number'. . Please advise me. 回答1: If you add an implicit (or explicit

Web API - JsonConverter - Custom Attribute

微笑、不失礼 提交于 2019-12-11 01:52:33
问题 We are having a web api project and inorder to convert the date time to date and vice versa, we are using DateTimeconverter extended from JsonConverter. We are using this in the form of an attribute for all the required DateTime properties (as shown below): [JsonConverter(typeof(CustomDateConverter))] The CustomDateConverter is as below: public class CustomDateConverter: JsonConverter { private string[] formats = new string[] { "yyyy-MM-dd", "MM/dd/yy", "MM/dd/yyyy", "dd-MMM-yy" }; public

Deserializing json with recursive types

Deadly 提交于 2019-12-11 01:30:16
问题 I've got some json that looks like this: [ { "MenuItem1": [ { "SubItem1": [ ] }, { "SubItem2": [ ] }, { "SubItem3": [ { "SubSubItem": [ ] } ] } ] }, { "MenuItem2": [ ] } ] This can be represented with the following C# data structure: class MenuItem { Dictionary<string, MenuItem[]> Items; } I tried deserializing this as MenuItem[] roots = JsonConvert.DeserializeObject<MenuItem[]>(json); but it doesn't work because it doesn't know that the Items member is where this dictionary data is supposed

Create list of objects from Json object with objects in xamarin for Android

梦想与她 提交于 2019-12-11 01:25:54
问题 I`m using JSON.net and I'm trying to get a list of Expense objects from JSON like this: {"ApplicationUser": null, "Currency": 1, "Description": "Moj pierwszy portfel", "Expenses": [{"Date": "2015-10-01T00:00:00", "Description": "Opis", "ExpenseType": {"Id": 1, "IsExpense": true, "Name": "Jedzenie"}, "ExpenseTypeId": 1, "Id": 1, "Name": "Biedronka", "Value": 40.00, "WalletId": 1}], "Id": 1, "Name": "Moj portfel", "Saldo": 100.12, "UserId": "f9b94a9a-44b3-4987-8711-6c1b73a5cb0e"} api url: http: