JSON deserialize dictionaries with varying keys - c#
I'd like to deserialize some JSON strings like this:
{"time":1506174868,"pairs":{
"AAA":{"books":8,"min":0.1,"max":1.0,"fee":0.01},
"AAX":{"books":8,"min":0.1,"max":1.0,"fee":0.01},
"AQA":{"books":8,"min":0.1,"max":1.0,"fee":0.01}
}}
where AAA, AAX, ... there are hundreds of variations
I paste this Json as class in VS2017 and get the following:
public class Rootobject
{
public int time { get; set; }
public Pairs pairs { get; set; }
}
public class Pairs
{
public AAA AAA { get; set; }
public AAX AAX { get; set; }
public AQA AQA { get; set; }
}
public class AAA
{
public int books { get; set; }
public float min { get; set; }
public float max { get; set; }
public float fee { get; set; }
}
public class AAX
{
public int books { get; set; }
public float min { get; set; }
public float max { get; set; }
public float fee { get; set; }
}
public class AQA
{
public int books { get; set; }
public float min { get; set; }
public float max { get; set; }
public float fee { get; set; }
}
I'd try to avoid hundreds of class declarations since all classes are same except
their name.
I tried to serialize this as array or list but I get exception since this is not an array.
I use Newtonsoft JSON lib.
Thank you
Sure, you can parse the json string to object as follows:
public class Rootobject
{
public int time { get; set; }
public Dictionary<string, ChildObject> pairs { get; set; }
}
public class ChildObject
{
public int books { get; set; }
public float min { get; set; }
public float max { get; set; }
public float fee { get; set; }
}
class Program
{
static string json = #"
{""time"":1506174868,""pairs"":{
""AAA"":{""books"":8,""min"":0.1,""max"":1.0,""fee"":0.01},
""AAX"":{""books"":8,""min"":0.1,""max"":1.0,""fee"":0.01},
""AQA"":{""books"":8,""min"":0.1,""max"":1.0,""fee"":0.01}
}
}";
static void Main(string[] args)
{
Rootobject root = JsonConvert.DeserializeObject<Rootobject>(json);
foreach(var child in root.pairs)
{
Console.WriteLine(string.Format("Key: {0}, books:{1},min:{2},max:{3},fee:{4}",
child.Key, child.Value.books, child.Value.max, child.Value.min, child.Value.fee));
}
}
thisextendsthat's answer is fine for your specific case. However, there are fully dynamic options for deserialization:
1) Parse into an JToken
var root = JObject.Parse(jsonString);
var time = root["time"];
2) Parse into a dynamic
dynamic d = JObject.Parse(jsonString);
var time = d.time;
Related
c# json with a changing class
public class 2500113075262000 { public string pair { get; set; } public string type { get; set; } public double amount { get; set; } public int rate { get; set; } public string timestamp_created { get; set; } public int status { get; set; } } public class Return { public 2500113075262000 2500113075262000 { get; set; } } public class Root { public int success { get; set; } public Return #return { get; set; } } class 2500113075262000 is constantly changing, this is the order ID, like deserialize {"success":1,"return":{"2500113075262000":{"pair":"eth_rur","type":"sell","amount":0.00110569,"rate":46100,"timestamp_created":"1608918997","status":0}}}
It looks like it's only the key - presumably the order ID - which is changing. I would suggest removing your Return class entirely, and changing your Root class to have a Dictionary<string, Order>. I'd also suggest writing your classes with idiomatic .NET property names, and using JsonPropertyAttribute to specify the representation in the JSON. So it would be something like this: public class Order { [JsonProperty("pair")] public string Pair { get; set; } [JsonProperty("type")] public string Type { get; set; } // etc, other properties } public class Root { [JsonProperty("success")] public int Success { get; set; } [JsonProperty("return")] public Dictionary<string, Order> Returns { get; set; } }
i can't deserializing into JSON in C#. Is it something wrong with my classes?
I'm having trouble when tryin to Deserialize a string into JSON. I'm making a call to a API. The anwer i get is stored i a variable(body) Root Api = JsonConvert.DeserializeObject<Root>(body); //var json = JsonConvert.DeserializeObject<Root>(body); public partial class All { public int matchsPlayed { get; set; } public int win { get; set; } public int draw { get; set; } public int lose { get; set; } public int goalsFor { get; set; } public int goalsAgainst { get; set; } } public partial class Home { public int matchsPlayed { get; set; } public int win { get; set; } public int draw { get; set; } public int lose { get; set; } public int goalsFor { get; set; } public int goalsAgainst { get; set; } } public partial class Away { public int matchsPlayed { get; set; } public int win { get; set; } public int draw { get; set; } public int lose { get; set; } public int goalsFor { get; set; } public int goalsAgainst { get; set; } } public partial class Standings // Offers { public int rank { get; set; } public int team_id { get; set; } public string teamName { get; set; } public string logo { get; set; } public string group { get; set; } public string forme { get; set; } public string status { get; set; } public string description { get; set; } public All all { get; set; } public Home home { get; set; } public Away away { get; set; } public int goalsDiff { get; set; } public int points { get; set; } public string lastUpdate { get; set; } } public partial class Api // ProductListing { public int results { get; set; } // public Standings[] Standings { get; set; } public List<Standings> standings { get; set; } } public partial class Root // RootObject { public Api api { get; set; } } I get this error message: " Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Standings' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. " This is the whole json text i'm tryin to deserialize: > {"api":{"results":1,"standings":[[{"rank":1,"team_id":45,"teamName":"Everton","logo":"https:\/\/media.api-sports.io\/football\/teams\/45.png","group":"Premier League","forme":"DWWWW","status":"same","description":"Promotion - Champions League (Group Stage)","all":{"matchsPlayed":5,"win":4,"draw":1,"lose":0,"goalsFor":14,"goalsAgainst":7},"home":{"matchsPlayed":3,"win":2,"draw":1,"lose":0,"goalsFor":11,"goalsAgainst":6},"away":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":3,"goalsAgainst":1},"goalsDiff":7,"points":13,"lastUpdate":"2020-10-19"},{"rank":2,"team_id":66,"teamName":"Aston Villa","logo":"https:\/\/media.api-sports.io\/football\/teams\/66.png","group":"Premier League","forme":"WWWW","status":"same","description":"Promotion - Champions League (Group Stage)","all":{"matchsPlayed":4,"win":4,"draw":0,"lose":0,"goalsFor":12,"goalsAgainst":2},"home":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":8,"goalsAgainst":2},"away":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":4,"goalsAgainst":0},"goalsDiff":10,"points":12,"lastUpdate":"2020-10-19"},{"rank":3,"team_id":40,"teamName":"Liverpool","logo":"https:\/\/media.api-sports.io\/football\/teams\/40.png","group":"Premier League","forme":"DLWWW","status":"same","description":"Promotion - Champions League (Group Stage)","all":{"matchsPlayed":5,"win":3,"draw":1,"lose":1,"goalsFor":13,"goalsAgainst":13},"home":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":7,"goalsAgainst":4},"away":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":6,"goalsAgainst":9},"goalsDiff":0,"points":10,"lastUpdate":"2020-10-19"},{"rank":4,"team_id":46,"teamName":"Leicester","logo":"https:\/\/media.api-sports.io\/football\/teams\/46.png","group":"Premier League","forme":"LLWWW","status":"same","description":"Promotion - Champions League (Group Stage)","all":{"matchsPlayed":5,"win":3,"draw":0,"lose":2,"goalsFor":12,"goalsAgainst":8},"home":{"matchsPlayed":3,"win":1,"draw":0,"lose":2,"goalsFor":4,"goalsAgainst":6},"away":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":8,"goalsAgainst":2},"goalsDiff":4,"points":9,"lastUpdate":"2020-10-19"},{"rank":5,"team_id":42,"teamName":"Arsenal","logo":"https:\/\/media.api-sports.io\/football\/teams\/42.png","group":"Premier League","forme":"LWLWW","status":"same","description":"Promotion - Europa League (Group Stage)","all":{"matchsPlayed":5,"win":3,"draw":0,"lose":2,"goalsFor":8,"goalsAgainst":6},"home":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":4,"goalsAgainst":2},"away":{"matchsPlayed":3,"win":1,"draw":0,"lose":2,"goalsFor":4,"goalsAgainst":4},"goalsDiff":2,"points":9,"lastUpdate":"2020-10-19"},{"rank":6,"team_id":39,"teamName":"Wolves","logo":"https:\/\/media.api-sports.io\/football\/teams\/39.png","group":"Premier League","forme":"WWLLW","status":"same","description":null,"all":{"matchsPlayed":5,"win":3,"draw":0,"lose":2,"goalsFor":5,"goalsAgainst":7},"home":{"matchsPlayed":2,"win":1,"draw":0,"lose":1,"goalsFor":2,"goalsAgainst":3},"away":{"matchsPlayed":3,"win":2,"draw":0,"lose":1,"goalsFor":3,"goalsAgainst":4},"goalsDiff":-2,"points":9,"lastUpdate":"2020-10-19"},{"rank":7,"team_id":47,"teamName":"Tottenham","logo":"https:\/\/media.api-sports.io\/football\/teams\/47.png","group":"Premier League","forme":"DWDWL","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":2,"lose":1,"goalsFor":15,"goalsAgainst":8},"home":{"matchsPlayed":3,"win":0,"draw":2,"lose":1,"goalsFor":4,"goalsAgainst":5},"away":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":11,"goalsAgainst":3},"goalsDiff":7,"points":8,"lastUpdate":"2020-10-19"},{"rank":8,"team_id":49,"teamName":"Chelsea","logo":"https:\/\/media.api-sports.io\/football\/teams\/49.png","group":"Premier League","forme":"DWDLW","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":2,"lose":1,"goalsFor":13,"goalsAgainst":9},"home":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":7,"goalsAgainst":5},"away":{"matchsPlayed":2,"win":1,"draw":1,"lose":0,"goalsFor":6,"goalsAgainst":4},"goalsDiff":4,"points":8,"lastUpdate":"2020-10-19"},{"rank":9,"team_id":48,"teamName":"West Ham","logo":"https:\/\/media.api-sports.io\/football\/teams\/48.png","group":"Premier League","forme":"DWWLL","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":1,"lose":2,"goalsFor":11,"goalsAgainst":7},"home":{"matchsPlayed":2,"win":1,"draw":0,"lose":1,"goalsFor":4,"goalsAgainst":2},"away":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":7,"goalsAgainst":5},"goalsDiff":4,"points":7,"lastUpdate":"2020-10-19"},{"rank":10,"team_id":63,"teamName":"Leeds","logo":"https:\/\/media.api-sports.io\/football\/teams\/63.png","group":"Premier League","forme":"LDWWL","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":1,"lose":2,"goalsFor":9,"goalsAgainst":9},"home":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":5,"goalsAgainst":5},"away":{"matchsPlayed":2,"win":1,"draw":0,"lose":1,"goalsFor":4,"goalsAgainst":4},"goalsDiff":0,"points":7,"lastUpdate":"2020-10-19"},{"rank":11,"team_id":50,"teamName":"Manchester City","logo":"https:\/\/media.api-sports.io\/football\/teams\/50.png","group":"Premier League","forme":"WDLW","status":"same","description":null,"all":{"matchsPlayed":4,"win":2,"draw":1,"lose":1,"goalsFor":7,"goalsAgainst":7},"home":{"matchsPlayed":2,"win":1,"draw":0,"lose":1,"goalsFor":3,"goalsAgainst":5},"away":{"matchsPlayed":2,"win":1,"draw":1,"lose":0,"goalsFor":4,"goalsAgainst":2},"goalsDiff":0,"points":7,"lastUpdate":"2020-10-19"},{"rank":12,"team_id":41,"teamName":"Southampton","logo":"https:\/\/media.api-sports.io\/football\/teams\/41.png","group":"Premier League","forme":"DWWLL","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":1,"lose":2,"goalsFor":8,"goalsAgainst":9},"home":{"matchsPlayed":2,"win":1,"draw":0,"lose":1,"goalsFor":4,"goalsAgainst":5},"away":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":4,"goalsAgainst":4},"goalsDiff":-1,"points":7,"lastUpdate":"2020-10-19"},{"rank":13,"team_id":34,"teamName":"Newcastle","logo":"https:\/\/media.api-sports.io\/football\/teams\/34.png","group":"Premier League","forme":"LWDLW","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":1,"lose":2,"goalsFor":7,"goalsAgainst":9},"home":{"matchsPlayed":3,"win":1,"draw":0,"lose":2,"goalsFor":4,"goalsAgainst":8},"away":{"matchsPlayed":2,"win":1,"draw":1,"lose":0,"goalsFor":3,"goalsAgainst":1},"goalsDiff":-2,"points":7,"lastUpdate":"2020-10-19"},{"rank":14,"team_id":52,"teamName":"Crystal Palace","logo":"https:\/\/media.api-sports.io\/football\/teams\/52.png","group":"Premier League","forme":"DLLWW","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":1,"lose":2,"goalsFor":6,"goalsAgainst":8},"home":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":3,"goalsAgainst":3},"away":{"matchsPlayed":2,"win":1,"draw":0,"lose":1,"goalsFor":3,"goalsAgainst":5},"goalsDiff":-2,"points":7,"lastUpdate":"2020-10-19"},{"rank":15,"team_id":33,"teamName":"Manchester United","logo":"https:\/\/media.api-sports.io\/football\/teams\/33.png","group":"Premier League","forme":"WLWL","status":"same","description":null,"all":{"matchsPlayed":4,"win":2,"draw":0,"lose":2,"goalsFor":9,"goalsAgainst":12},"home":{"matchsPlayed":2,"win":0,"draw":0,"lose":2,"goalsFor":2,"goalsAgainst":9},"away":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":7,"goalsAgainst":3},"goalsDiff":-3,"points":6,"lastUpdate":"2020-10-19"},{"rank":16,"team_id":51,"teamName":"Brighton","logo":"https:\/\/media.api-sports.io\/football\/teams\/51.png","group":"Premier League","forme":"DLLWL","status":"same","description":null,"all":{"matchsPlayed":5,"win":1,"draw":1,"lose":3,"goalsFor":9,"goalsAgainst":11},"home":{"matchsPlayed":2,"win":0,"draw":0,"lose":2,"goalsFor":3,"goalsAgainst":6},"away":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":6,"goalsAgainst":5},"goalsDiff":-2,"points":4,"lastUpdate":"2020-10-19"},{"rank":17,"team_id":60,"teamName":"West Brom","logo":"https:\/\/media.api-sports.io\/football\/teams\/60.png","group":"Premier League","forme":"DLDLL","status":"same","description":null,"all":{"matchsPlayed":5,"win":0,"draw":2,"lose":3,"goalsFor":5,"goalsAgainst":13},"home":{"matchsPlayed":3,"win":0,"draw":2,"lose":1,"goalsFor":3,"goalsAgainst":6},"away":{"matchsPlayed":2,"win":0,"draw":0,"lose":2,"goalsFor":2,"goalsAgainst":7},"goalsDiff":-8,"points":2,"lastUpdate":"2020-10-19"},{"rank":18,"team_id":44,"teamName":"Burnley","logo":"https:\/\/media.api-sports.io\/football\/teams\/44.png","group":"Premier League","forme":"DLLL","status":"same","description":"Relegation - Championship","all":{"matchsPlayed":4,"win":0,"draw":1,"lose":3,"goalsFor":3,"goalsAgainst":8},"home":{"matchsPlayed":1,"win":0,"draw":0,"lose":1,"goalsFor":0,"goalsAgainst":1},"away":{"matchsPlayed":3,"win":0,"draw":1,"lose":2,"goalsFor":3,"goalsAgainst":7},"goalsDiff":-5,"points":1,"lastUpdate":"2020-10-19"},{"rank":19,"team_id":62,"teamName":"Sheffield Utd","logo":"https:\/\/media.api-sports.io\/football\/teams\/62.png","group":"Premier League","forme":"DLLLL","status":"same","description":"Relegation - Championship","all":{"matchsPlayed":5,"win":0,"draw":1,"lose":4,"goalsFor":2,"goalsAgainst":7},"home":{"matchsPlayed":3,"win":0,"draw":1,"lose":2,"goalsFor":1,"goalsAgainst":4},"away":{"matchsPlayed":2,"win":0,"draw":0,"lose":2,"goalsFor":1,"goalsAgainst":3},"goalsDiff":-5,"points":1,"lastUpdate":"2020-10-19"},{"rank":20,"team_id":36,"teamName":"Fulham","logo":"https:\/\/media.api-sports.io\/football\/teams\/36.png","group":"Premier League","forme":"DLLLL","status":"same","description":"Relegation - Championship","all":{"matchsPlayed":5,"win":0,"draw":1,"lose":4,"goalsFor":4,"goalsAgainst":12},"home":{"matchsPlayed":2,"win":0,"draw":0,"lose":2,"goalsFor":0,"goalsAgainst":6},"away":{"matchsPlayed":3,"win":0,"draw":1,"lose":2,"goalsFor":4,"goalsAgainst":6},"goalsDiff":-8,"points":1,"lastUpdate":"2020-10-19"}]]}}
If you change the definition of Api so standings is a list of lists it should work as the JSON has an array of arrays. public partial class Api // ProductListing { public int results { get; set; } public List<List<Standings>> standings { get; set; } }
Deserialize a .json string returns: 'Object reference not set to an instance of an object'
I am trying to read a .json response. I have pasted the response here: https://pastebin.com/0Zgg39si Then I use the code below. When I run the code, I get the below error for: "var deserializedTickers" System.NullReferenceException: 'Object reference not set to an instance of an object.' The code is the below. I am not sure what is causing this? using Newtonsoft.Json.Linq; using Newtonsoft.Json; public void test() { //responseBody holds the .json response String responseBody = ""; var deserializedTickers = JsonConvert.DeserializeObject<TickersRoot>(responseBody); foreach (var ticker in deserializedTickers.Tickers) { var symbol2 = ticker.Value.Symbol; } } public class TickersRoot { public Dictionary<string, Ticker> Tickers { get; set; } } public class Ticker { public string Symbol { get; set; } public long Timestamp { get; set; } public DateTime Datetime { get; set; } public double High { get; set; } public double Low { get; set; } public double Bid { get; set; } public double Ask { get; set; } public double Vwap { get; set; } public double Open { get; set; } public double Close { get; set; } public double Last { get; set; } public double BaseVolume { get; set; } public double QuoteVolume { get; set; } public Info Info { get; set; } } public class Info { public List<string> a { get; set; } public List<string> b { get; set; } public List<string> c { get; set; } public List<string> v { get; set; } public List<string> p { get; set; } public List<int> t { get; set; } public List<string> l { get; set; } public List<string> h { get; set; } public string o { get; set; } }
Based on the response, your Info class should be something like this (set the datatype to match your needs): public class Info { public string Buy { get; set; } public string Sell { get; set; } public string Open { get; set; } public string Low { get; set; } public string High { get; set; } public string Last { get; set; } public string Vol { get; set; } } as you don't have a property called "Tickers" on the json body, call the JsonConver.DeserializeObject method like this: var deserializedTickers = JsonConvert.DeserializeObject<Dictionary<string, Ticker>>(responseBody); then you can iterate the result as: foreach (var ticker in deserializedTickers) { var symbol2 = ticker.Value.Symbol; }
I had this error when one of my [Serializable] objects only had 1 constructor with a required arg. When de-serializing, the newtonsoft.json package could not create the entity from the data, since it had a required param in its constructor. I solved it by removing the constructor and remembering to call a helper function when instantiating objects that are not loaded from a file/json.
You can either change the root json object to have a property named "tickers" that encapsulates the dictionary { "tickers":{ "BTC/AUD": { ... }, ... } } Or deserialize the original json directly into a dictionary var deserializedTickers = JsonConvert.DeserializeObject<Dictionary<string, Ticker>>(responseBody); You should also change the Info class to match the json schema
Deserialize JSON to a class (Newtonsoft, C#)
I am trying to save JSON data into my Class. I've tried different ways but didn't succeed. Here is my class: class YoCurrency { public string Currency { get; set; } public double high { get; set; } public double low { get; set; } public double avg { get; set; } public double vol { get; set; } [JsonProperty("vol_cur")] public double vol_cur { get; set; } public double last { get; set; } public double buy { get; set; } public double sell { get; set; } public int updated { get; set; } } Here is JSON file. Trying to deserialize with generic List: List<YoCurrency> a = JsonConvert.DeserializeObject<List<YoCurrency>>(json); gives an error: Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[YoBitParser.YobitCurrency]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'btc_usd', line 1, position 11.' Deserializing this way: var a = JsonConvert.DeserializeObject<YobitCurrency>(json); Doesn't give any result, because all class fields get 0 value Also I tried to use two classes like this: class YoCurrency { public double high { get; set; } public double low { get; set; } public double avg { get; set; } public double vol { get; set; } [JsonProperty("vol_cur")] public double vol_cur { get; set; } public double last { get; set; } public double buy { get; set; } public double sell { get; set; } public int updated { get; set; } } class YoPair { YoCurrency Currency { get; set; } string Name { get; set; } } But it didn't give any positive result. I tried to generate c# classes using http://json2csharp.com, but it generates a class for every currency: public class BtcUsd { public double high { get; set; } public double low { get; set; } public double avg { get; set; } public double vol { get; set; } public double vol_cur { get; set; } public double last { get; set; } public double buy { get; set; } public double sell { get; set; } public int updated { get; set; } } public class EthUsd { ... } public class RootObject { public BtcUsd btc_usd { get; set; } public EthUsd eth_usd { get; set; } ... } But i suppose it's not good way in case of 50 or 500 pairs (or should I create a unique class for every currency?) So please help me to deserialize this JSON or give me some information that could help me to solve this problem.
Why not just make one class? class Program { static void Main(string[] args) { var array = "[{\"btc_usd\":[{\"high\":8550.0102,\"low\":7800,\"avg\":8175.0051,\"vol\":1615543.57397705,\"vol_cur\":197.54076079,\"last\":7850,\"buy\":7850.00000000,\"sell\":7879.00000000,\"updated\":1521383863}],\"eth_usd\":[{\"high\":622.93708559,\"low\":482,\"avg\":552.46854279,\"vol\":346598.40520112,\"vol_cur\":630.37075493,\"last\":488.27857735,\"buy\":489.77564548,\"sell\":492.11726255,\"updated\":1521383876}]}]"; List<RootObject> a = JsonConvert.DeserializeObject<List<RootObject>>(array); } } public class RootCurrency { public double high { get; set; } public int low { get; set; } public double avg { get; set; } public double vol { get; set; } public double vol_cur { get; set; } public double last { get; set; } public double buy { get; set; } public double sell { get; set; } public int updated { get; set; } } public class RootObject { public List<RootCurrency> btc_usd { get; set; } public List<RootCurrency> eth_usd { get; set; } }
It look like your class structure is not proper, suggest you to generate class with the help of Visual Studio like as below and try your code by using generated class based on your json strucutre you need class design like this public class Rootobject { // do List<CurrencyDetail> if you are expecting multiple element public CurrencyDetail btc_usd { get; set; } public CurrencyDetail eth_usd { get; set; } public CurrencyDetail doge_usd { get; set; } public CurrencyDetail pac_usd { get; set; } public CurrencyDetail rdd_usd { get; set; } } public class CurrencyDetail { public float high { get; set; } public int low { get; set; } public float avg { get; set; } public float vol { get; set; } public float vol_cur { get; set; } public float last { get; set; } public float buy { get; set; } public float sell { get; set; } public int updated { get; set; } } my suggesting is if possible modify you json , which will contain currency type in currency detail than you class strucutre will be like public class Rootobject { public List<CurrencyDetail> currencies { get; set; } } public class CurrencyDetail { //btc_usd or eth_usd or doge_usd or pac_usd public string type { get; set; } public float high { get; set; } public int low { get; set; } public float avg { get; set; } public float vol { get; set; } public float vol_cur { get; set; } public float last { get; set; } public float buy { get; set; } public float sell { get; set; } public int updated { get; set; } }
Deserialization of json string returns null values
how can I deserialize the ff json string: {"stock":[{"name":"stock1","price":{"currency":"AUD","amount":103.50},"percent_change":-1.33,"volume":1583760,"symbol":"SC1"}],"as_of":"2016-06-10T15:20:00+08:00"} I've tried the code: JsonConvert.DeserializeObject<stock>(content); where content variable is the json string above. However I am getting null value of the properties. Here are my classes: public class price { public string currency { get; } public double amount { get; } } public class stock { public string name { get; } public price price { get; } public double percent_change { get; } public int volume { get; } public string symbol { get; } } Thank you in advance!
Add a setter: public string name { get; set; } -- update -- You are putting a list of stock into stock. Add the class: public class container { public List<stock> Stock { get; set; } public string as_of { get; set; } } And call: var result = JsonConvert.DeserializeObject<container>(content);
Use this class for your json string- public class Price { public string currency { get; set; } public double amount { get; set; } } public class Stock { public string name { get; set; } public Price price { get; set; } public double percent_change { get; set; } public int volume { get; set; } public string symbol { get; set; } } public class StockDetails { public List<Stock> stock { get; set; } public string as_of { get; set; } }