I am calling an API which is giving me a json response like
{
"symbol": "AAPL",
"stock_exchange_short": "NASDAQ",
"timezone_name": "America/New_York",
"intraday": {
"2018-11-21 15:59:00": {
"open": "177.24",
"close": "176.77",
"high": "177.25",
"low": "176.77",
"volume": "430073"
},
"2018-11-21 15:58:00": {
"open": "177.23",
"close": "177.23",
"high": "177.25",
"low": "177.12",
"volume": "188425"
},
"2018-11-21 15:57:00": {
"open": "177.18",
"close": "177.21",
"high": "177.24",
"low": "177.11",
"volume": "163151"
},
Now I want to access all data so I need to create an object of this but when I am using Json2cSharp converter then it gives me an object name which is invalid type. So which type of object I should make so I can access all data regularly. Please help.
-
Use VisualStudio's "Edit -> Paste Special -> Paste JSON As Classes", it will generate necessary classes for you.SᴇM– SᴇM2018年11月23日 13:21:52 +00:00Commented Nov 23, 2018 at 13:21
-
@SeM, I couldnt find that Paste JSON as Classes in VS2013. Can you help me out?Pranesh Janarthanan– Pranesh Janarthanan2018年11月24日 10:58:11 +00:00Commented Nov 24, 2018 at 10:58
2 Answers 2
You could use something like this:
public partial class Welcome
{
[JsonProperty("symbol")]
public string Symbol { get; set; }
[JsonProperty("stock_exchange_short")]
public string StockExchangeShort { get; set; }
[JsonProperty("timezone_name")]
public string TimezoneName { get; set; }
[JsonProperty("intraday")]
public Dictionary<string, Intraday> Intraday { get; set; }
}
public partial class Intraday
{
[JsonProperty("open")]
public string Open { get; set; }
[JsonProperty("close")]
public string Close { get; set; }
[JsonProperty("high")]
public string High { get; set; }
[JsonProperty("low")]
public string Low { get; set; }
[JsonProperty("volume")]
public long Volume { get; set; }
}
The tricky part is the Intraday
property, because you have to use a dictionary to get all the values correctly.
I've used quicktype (which json2csharp is now joining forces with). If you want to play yourself a bit with the tool here's a link to the code: https://app.quicktype.io?share=DRgQz3PJVCLy4JR3JtGZ
There's a lot more code there if you change options in the right menu. You can set the Output Features to Complete
and will get a really nice snippet. Including the usage. In that case, something like the below will be enough to get the json deserialized to your custom class.
var welcome = Welcome.FromJson(jsonString);
Hope this helps!
-
1Thank you @Karel Tamayo. It's working better and providing data as per our expectations. Thank you.Hardik Dhankecha– Hardik Dhankecha2018年11月28日 09:45:52 +00:00Commented Nov 28, 2018 at 9:45
I have recently faced same issues from SMS report API, i have asked them to modify response to the below object style. Converting a json array to C# array object is not possible under DeserializeObject. so I prefered List data structure.
public class APIResponse
{
public string symbol { get; set; }
public string stock_exchange_short { get; set; }
public string timezone_name { get; set; }
public List<IntradayLog> intraday { get; set; }
}
public class IntradayLog
{
public float open { get; set; }
public float close { get; set; }
public float high { get; set; }
public float low { get; set; }
public int volume { get; set; }
public DateTime Date { get; set; }
}
var apiLogJson = JsonConvert.DeserializeObject<APIResponse>(myAPIResponse);
Update @Sem commets to use EDit => Paste Special => Paste Json As Classes, i got this
public class Rootobject
{
public string symbol { get; set; }
public string stock_exchange_short { get; set; }
public string timezone_name { get; set; }
public Intraday intraday { get; set; }
}
public class Intraday
{
public _20181121155900 _20181121155900 { get; set; }
public _20181121155800 _20181121155800 { get; set; }
public _20181121155700 _20181121155700 { get; set; }
}
public class _20181121155900
{
public string open { get; set; }
public string close { get; set; }
public string high { get; set; }
public string low { get; set; }
public string volume { get; set; }
}
public class _20181121155800
{
public string open { get; set; }
public string close { get; set; }
public string high { get; set; }
public string low { get; set; }
public string volume { get; set; }
}
public class _20181121155700
{
public string open { get; set; }
public string close { get; set; }
public string high { get; set; }
public string low { get; set; }
public string volume { get; set; }
}