Stuck with DateTime Object name in Json API call
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.
c# asp.net json asp.net-mvc api
add a comment |
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.
c# asp.net json asp.net-mvc api
Use VisualStudio's "Edit -> Paste Special -> Paste JSON As Classes", it will generate necessary classes for you.
– SeM
Nov 23 '18 at 13:21
@SeM, I couldnt find that Paste JSON as Classes in VS2013. Can you help me out?
– Pranesh Janarthanan
Nov 24 '18 at 10:58
add a comment |
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.
c# asp.net json asp.net-mvc api
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.
c# asp.net json asp.net-mvc api
c# asp.net json asp.net-mvc api
asked Nov 23 '18 at 13:09
Hardik DhankechaHardik Dhankecha
618
618
Use VisualStudio's "Edit -> Paste Special -> Paste JSON As Classes", it will generate necessary classes for you.
– SeM
Nov 23 '18 at 13:21
@SeM, I couldnt find that Paste JSON as Classes in VS2013. Can you help me out?
– Pranesh Janarthanan
Nov 24 '18 at 10:58
add a comment |
Use VisualStudio's "Edit -> Paste Special -> Paste JSON As Classes", it will generate necessary classes for you.
– SeM
Nov 23 '18 at 13:21
@SeM, I couldnt find that Paste JSON as Classes in VS2013. Can you help me out?
– Pranesh Janarthanan
Nov 24 '18 at 10:58
Use VisualStudio's "Edit -> Paste Special -> Paste JSON As Classes", it will generate necessary classes for you.
– SeM
Nov 23 '18 at 13:21
Use VisualStudio's "Edit -> Paste Special -> Paste JSON As Classes", it will generate necessary classes for you.
– SeM
Nov 23 '18 at 13:21
@SeM, I couldnt find that Paste JSON as Classes in VS2013. Can you help me out?
– Pranesh Janarthanan
Nov 24 '18 at 10:58
@SeM, I couldnt find that Paste JSON as Classes in VS2013. Can you help me out?
– Pranesh Janarthanan
Nov 24 '18 at 10:58
add a comment |
2 Answers
2
active
oldest
votes
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!
1
Thank you @Karel Tamayo. It's working better and providing data as per our expectations. Thank you.
– Hardik Dhankecha
Nov 28 '18 at 9:45
add a comment |
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
How to Paste
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; }
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447346%2fstuck-with-datetime-object-name-in-json-api-call%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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!
1
Thank you @Karel Tamayo. It's working better and providing data as per our expectations. Thank you.
– Hardik Dhankecha
Nov 28 '18 at 9:45
add a comment |
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!
1
Thank you @Karel Tamayo. It's working better and providing data as per our expectations. Thank you.
– Hardik Dhankecha
Nov 28 '18 at 9:45
add a comment |
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!
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!
edited Nov 23 '18 at 13:39
answered Nov 23 '18 at 13:25
Karel TamayoKarel Tamayo
2,71721522
2,71721522
1
Thank you @Karel Tamayo. It's working better and providing data as per our expectations. Thank you.
– Hardik Dhankecha
Nov 28 '18 at 9:45
add a comment |
1
Thank you @Karel Tamayo. It's working better and providing data as per our expectations. Thank you.
– Hardik Dhankecha
Nov 28 '18 at 9:45
1
1
Thank you @Karel Tamayo. It's working better and providing data as per our expectations. Thank you.
– Hardik Dhankecha
Nov 28 '18 at 9:45
Thank you @Karel Tamayo. It's working better and providing data as per our expectations. Thank you.
– Hardik Dhankecha
Nov 28 '18 at 9:45
add a comment |
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
How to Paste
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; }
}
add a comment |
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
How to Paste
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; }
}
add a comment |
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
How to Paste
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; }
}
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
How to Paste
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; }
}
edited Nov 24 '18 at 11:10
answered Nov 23 '18 at 13:52
Pranesh JanarthananPranesh Janarthanan
489616
489616
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447346%2fstuck-with-datetime-object-name-in-json-api-call%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Use VisualStudio's "Edit -> Paste Special -> Paste JSON As Classes", it will generate necessary classes for you.
– SeM
Nov 23 '18 at 13:21
@SeM, I couldnt find that Paste JSON as Classes in VS2013. Can you help me out?
– Pranesh Janarthanan
Nov 24 '18 at 10:58