json.net

Iterating JObject Keys

不问归期 提交于 2019-12-14 03:04:05
问题 I have the following JObject as return by https://gate.io/api2#trade API. How do I iterate through each key which is a separate coin also get its value. I tried to parse it using Newtonsoft JObject Parse like this: var coinData = JObject.Parse(@"{ ""result"": ""true"", ""available"": { ""BTC"": ""0.83337671"", ""LTC"": ""94.364"", ""ETH"": ""0.07161"", ""ETC"": ""82.35029899"" }, ""locked"": { ""BTC"": ""0.0002"", ""YAC"": ""10.01"" } }")["available"]; foreach (JToken item in coinData) { item

Only serialize some specific properties of a class?

那年仲夏 提交于 2019-12-14 02:47:02
问题 I ran into a problem with Json.Net. My application is connected to a database and stores some data in it. Lets assume my application has three different classes: Entity, Group, Organisation Short look into Entity class: public class Entity { [JsonIgnore] public int ID { get; private set; } [JsonProperty] public string UID { get; private set; } [JsonProperty] public Gender EntityGender { get; set; } [JsonProperty] public string Surname { get; set; } [JsonProperty] public string Forename { get;

Deserialize JSON String into DataTable / DataSet using c#

对着背影说爱祢 提交于 2019-12-14 02:42:49
问题 I have following JSON String { "1":[ {"cityId":93,"cityName":"Tapah","cityCode":"TAP"}, {"cityId":3,"cityName":"Melaka","cityCode":"MLK"}, {"cityId":6,"cityName":"Kota Bharu","cityCode":"KB"}, {"cityId":7,"cityName":"Dungun","cityCode":"DG"} ], "2":[ {"cityId":77,"cityName":"Grik","cityCode":"GRIK"}, {"cityId":6,"cityName":"Kota Bharu","cityCode":"KB"}, {"cityId":7,"cityName":"Dungun","cityCode":"DG"}, {"cityId":98,"cityName":"Bangan Serai","cityCode":"BS"} ], "6":[ {"cityId":3,"cityName":

Json.Net - Explicitly include a single private property

安稳与你 提交于 2019-12-14 02:35:31
问题 I have an object (ViewModel) in which I have a private property that holds some meta-data about the model. I would like to explicitly include this private property in the serializing/deserializing of the object. Is there a Json.Net Attribute I can add to the property? public class Customer { private string TimeStamp { get; set; } public Guid CustomerId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Name { get { return FirstName + " " +

How does Serialize and De-serialize work internally? [closed]

浪子不回头ぞ 提交于 2019-12-14 02:21:16
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I have a lot of legacy code where json is parsed manually by using a for loop. This takes O(n) time in general. I know json.net would

Bind observable dictionary to listbox items. [WPF-C#]

99封情书 提交于 2019-12-14 02:17:51
问题 I try bind observable dictionary to listbox item, and my problem is. If I use normal generic dictionary, it works good. But if I use observable dictionary, listbox items are not loaded. Here is observable dictionary class: public class MyObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, INotifyCollectionChanged, INotifyPropertyChanged { private readonly IDictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>(); #region Implementation of INotifyCollectionChanged

How to inherit the attribute from interface to object when serializing it using JSON.NET [duplicate]

感情迁移 提交于 2019-12-14 01:25:10
问题 This question already has an answer here : Getting SerializeObject to use JsonProperty “name” defined inside interface (1 answer) Closed 8 months ago . using System; using System.Collections.Generic; using Newtonsoft.Json; using System.Text; using System.IO; using System.Runtime.Serialization; using Newtonsoft.Json.Serialization; using System.Linq; using System.Reflection; public interface IParent { [JsonProperty] int Id {get;set;} } [JsonObject(MemberSerialization.OptIn)] public class Parent

How to compare two Json objects using C#

ⅰ亾dé卋堺 提交于 2019-12-14 00:23:20
问题 I have two Json objects as below need to be compared. I am using Newtonsoft libraries for Json parsing. string InstanceExpected = jsonExpected; string InstanceActual = jsonActual; var InstanceObjExpected = JObject.Parse(InstanceExpected); var InstanceObjActual = JObject.Parse(InstanceActual); And I am using Fluent Assertions to compare it. But the problem is Fluent assertion fails only when the attribute count/names are not matching. If the json values are different it passes. I require to

JSON.Net Seralize values don't have quotation marks

人走茶凉 提交于 2019-12-13 22:55:07
问题 The following code: var json = JsonConvert.SerializeObject(fooObject, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, }); Seralizes a json (string) object of: { "fooA":0, "fooB":0, "fooC":false, "fooD":false, "fooE":0, "fooF":0, } The values are not in quotation marks, such as "fooA":"0". This is the behavior that I want. Is there a way to enforce this behavior? 回答1: In JSON format, numbers and booleans do not have quotes around them, while strings do (see JSON.org)

Convert JSON with illegal characters in property name [duplicate]

给你一囗甜甜゛ 提交于 2019-12-13 22:20:02
问题 This question already has answers here : How can I parse a JSON string that would cause illegal C# identifiers? (2 answers) Closed 3 years ago . Beforehand; I'm using an old version of Newtonsoft.Json (4.0.8.0). So I'm trying to write a .NET client for a webserver application. To convert all incoming packets from a json structure to .NET object i do use the JSON Serializer with the inbuilt function JToken.ToObject. This requires the target .net class to have the needed attributes named