How can I Deserialize this json? It is giving all null value with following codes
up vote
0
down vote
favorite
{
"Global Quote": {
"01. symbol": "MSFT",
"02. latest trading day": "2018-11-19",
"03. previous close": "108.2900"}
}
This is the code I m using:
using (var reader = new StreamReader(response.GetResponseStream()))
{
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
//This line is not working.
Stock stock = (Stock)js.Deserialize(objText, typeof(Stock));
return Ok(stock);
}
The stock is model as shown below:
public class Stock
{
public string symbol { get; set; }
public string latesttradingday { get; set; }
public string previousclose { get; set; }
}
All I m getting is Null in all field of Stock. What I m missing here? I m new to JSON.
c# json
add a comment |
up vote
0
down vote
favorite
{
"Global Quote": {
"01. symbol": "MSFT",
"02. latest trading day": "2018-11-19",
"03. previous close": "108.2900"}
}
This is the code I m using:
using (var reader = new StreamReader(response.GetResponseStream()))
{
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
//This line is not working.
Stock stock = (Stock)js.Deserialize(objText, typeof(Stock));
return Ok(stock);
}
The stock is model as shown below:
public class Stock
{
public string symbol { get; set; }
public string latesttradingday { get; set; }
public string previousclose { get; set; }
}
All I m getting is Null in all field of Stock. What I m missing here? I m new to JSON.
c# json
1
The json has field01. symbol. That can not be converted to propertysymbolof class.
– Chetan Ranpariya
Nov 20 at 4:34
This is funky json
– TheGeneral
Nov 20 at 4:35
So there is not any solution for this?
– Ujwal Neupane
Nov 20 at 4:38
@ChetanRanpariya is there any way to get value 108.2900 only from above data? That would work for me.
– Ujwal Neupane
Nov 20 at 4:43
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
{
"Global Quote": {
"01. symbol": "MSFT",
"02. latest trading day": "2018-11-19",
"03. previous close": "108.2900"}
}
This is the code I m using:
using (var reader = new StreamReader(response.GetResponseStream()))
{
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
//This line is not working.
Stock stock = (Stock)js.Deserialize(objText, typeof(Stock));
return Ok(stock);
}
The stock is model as shown below:
public class Stock
{
public string symbol { get; set; }
public string latesttradingday { get; set; }
public string previousclose { get; set; }
}
All I m getting is Null in all field of Stock. What I m missing here? I m new to JSON.
c# json
{
"Global Quote": {
"01. symbol": "MSFT",
"02. latest trading day": "2018-11-19",
"03. previous close": "108.2900"}
}
This is the code I m using:
using (var reader = new StreamReader(response.GetResponseStream()))
{
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
//This line is not working.
Stock stock = (Stock)js.Deserialize(objText, typeof(Stock));
return Ok(stock);
}
The stock is model as shown below:
public class Stock
{
public string symbol { get; set; }
public string latesttradingday { get; set; }
public string previousclose { get; set; }
}
All I m getting is Null in all field of Stock. What I m missing here? I m new to JSON.
c# json
c# json
edited Nov 20 at 5:16
TheGeneral
26.6k63163
26.6k63163
asked Nov 20 at 4:30
Ujwal Neupane
62
62
1
The json has field01. symbol. That can not be converted to propertysymbolof class.
– Chetan Ranpariya
Nov 20 at 4:34
This is funky json
– TheGeneral
Nov 20 at 4:35
So there is not any solution for this?
– Ujwal Neupane
Nov 20 at 4:38
@ChetanRanpariya is there any way to get value 108.2900 only from above data? That would work for me.
– Ujwal Neupane
Nov 20 at 4:43
add a comment |
1
The json has field01. symbol. That can not be converted to propertysymbolof class.
– Chetan Ranpariya
Nov 20 at 4:34
This is funky json
– TheGeneral
Nov 20 at 4:35
So there is not any solution for this?
– Ujwal Neupane
Nov 20 at 4:38
@ChetanRanpariya is there any way to get value 108.2900 only from above data? That would work for me.
– Ujwal Neupane
Nov 20 at 4:43
1
1
The json has field
01. symbol. That can not be converted to property symbol of class.– Chetan Ranpariya
Nov 20 at 4:34
The json has field
01. symbol. That can not be converted to property symbol of class.– Chetan Ranpariya
Nov 20 at 4:34
This is funky json
– TheGeneral
Nov 20 at 4:35
This is funky json
– TheGeneral
Nov 20 at 4:35
So there is not any solution for this?
– Ujwal Neupane
Nov 20 at 4:38
So there is not any solution for this?
– Ujwal Neupane
Nov 20 at 4:38
@ChetanRanpariya is there any way to get value 108.2900 only from above data? That would work for me.
– Ujwal Neupane
Nov 20 at 4:43
@ChetanRanpariya is there any way to get value 108.2900 only from above data? That would work for me.
– Ujwal Neupane
Nov 20 at 4:43
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Use JsonProperty
public class GlobalQuote
{
[JsonProperty("Global Quote")]
public Stock Stock { get; set; }
}
public class Stock
{
[JsonProperty("01. symbol")]
public string symbol { get; set; }
[JsonProperty("02. latest trading day")]
public string latesttradingday { get; set; }
[JsonProperty("03. previous close")]
public string previousclose { get; set; }
}
private static async Task Main(string args)
{
string test = @"{
""Global Quote"": {
""01. symbol"": ""MSFT"",
""02. latest trading day"": ""2018-11-19"",
""03. previous close"": ""108.2900""}
}";
var result = JsonConvert.DeserializeObject<GlobalQuote>(test);
}
Full Demo Here
Additional Resources
JsonProperty Class
Maps a JSON property to a .NET member or constructor parameter.
1
Good solution for indeed a funky Json, normally it is for mapping the names usingJsonProperty, but here we can't even form a class from default Json, +1
– Mrinal Kamboj
Nov 20 at 5:11
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%2f53386263%2fhow-can-i-deserialize-this-json-it-is-giving-all-null-value-with-following-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Use JsonProperty
public class GlobalQuote
{
[JsonProperty("Global Quote")]
public Stock Stock { get; set; }
}
public class Stock
{
[JsonProperty("01. symbol")]
public string symbol { get; set; }
[JsonProperty("02. latest trading day")]
public string latesttradingday { get; set; }
[JsonProperty("03. previous close")]
public string previousclose { get; set; }
}
private static async Task Main(string args)
{
string test = @"{
""Global Quote"": {
""01. symbol"": ""MSFT"",
""02. latest trading day"": ""2018-11-19"",
""03. previous close"": ""108.2900""}
}";
var result = JsonConvert.DeserializeObject<GlobalQuote>(test);
}
Full Demo Here
Additional Resources
JsonProperty Class
Maps a JSON property to a .NET member or constructor parameter.
1
Good solution for indeed a funky Json, normally it is for mapping the names usingJsonProperty, but here we can't even form a class from default Json, +1
– Mrinal Kamboj
Nov 20 at 5:11
add a comment |
up vote
3
down vote
accepted
Use JsonProperty
public class GlobalQuote
{
[JsonProperty("Global Quote")]
public Stock Stock { get; set; }
}
public class Stock
{
[JsonProperty("01. symbol")]
public string symbol { get; set; }
[JsonProperty("02. latest trading day")]
public string latesttradingday { get; set; }
[JsonProperty("03. previous close")]
public string previousclose { get; set; }
}
private static async Task Main(string args)
{
string test = @"{
""Global Quote"": {
""01. symbol"": ""MSFT"",
""02. latest trading day"": ""2018-11-19"",
""03. previous close"": ""108.2900""}
}";
var result = JsonConvert.DeserializeObject<GlobalQuote>(test);
}
Full Demo Here
Additional Resources
JsonProperty Class
Maps a JSON property to a .NET member or constructor parameter.
1
Good solution for indeed a funky Json, normally it is for mapping the names usingJsonProperty, but here we can't even form a class from default Json, +1
– Mrinal Kamboj
Nov 20 at 5:11
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Use JsonProperty
public class GlobalQuote
{
[JsonProperty("Global Quote")]
public Stock Stock { get; set; }
}
public class Stock
{
[JsonProperty("01. symbol")]
public string symbol { get; set; }
[JsonProperty("02. latest trading day")]
public string latesttradingday { get; set; }
[JsonProperty("03. previous close")]
public string previousclose { get; set; }
}
private static async Task Main(string args)
{
string test = @"{
""Global Quote"": {
""01. symbol"": ""MSFT"",
""02. latest trading day"": ""2018-11-19"",
""03. previous close"": ""108.2900""}
}";
var result = JsonConvert.DeserializeObject<GlobalQuote>(test);
}
Full Demo Here
Additional Resources
JsonProperty Class
Maps a JSON property to a .NET member or constructor parameter.
Use JsonProperty
public class GlobalQuote
{
[JsonProperty("Global Quote")]
public Stock Stock { get; set; }
}
public class Stock
{
[JsonProperty("01. symbol")]
public string symbol { get; set; }
[JsonProperty("02. latest trading day")]
public string latesttradingday { get; set; }
[JsonProperty("03. previous close")]
public string previousclose { get; set; }
}
private static async Task Main(string args)
{
string test = @"{
""Global Quote"": {
""01. symbol"": ""MSFT"",
""02. latest trading day"": ""2018-11-19"",
""03. previous close"": ""108.2900""}
}";
var result = JsonConvert.DeserializeObject<GlobalQuote>(test);
}
Full Demo Here
Additional Resources
JsonProperty Class
Maps a JSON property to a .NET member or constructor parameter.
edited Nov 20 at 4:53
answered Nov 20 at 4:46
TheGeneral
26.6k63163
26.6k63163
1
Good solution for indeed a funky Json, normally it is for mapping the names usingJsonProperty, but here we can't even form a class from default Json, +1
– Mrinal Kamboj
Nov 20 at 5:11
add a comment |
1
Good solution for indeed a funky Json, normally it is for mapping the names usingJsonProperty, but here we can't even form a class from default Json, +1
– Mrinal Kamboj
Nov 20 at 5:11
1
1
Good solution for indeed a funky Json, normally it is for mapping the names using
JsonProperty, but here we can't even form a class from default Json, +1– Mrinal Kamboj
Nov 20 at 5:11
Good solution for indeed a funky Json, normally it is for mapping the names using
JsonProperty, but here we can't even form a class from default Json, +1– Mrinal Kamboj
Nov 20 at 5:11
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53386263%2fhow-can-i-deserialize-this-json-it-is-giving-all-null-value-with-following-code%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
1
The json has field
01. symbol. That can not be converted to propertysymbolof class.– Chetan Ranpariya
Nov 20 at 4:34
This is funky json
– TheGeneral
Nov 20 at 4:35
So there is not any solution for this?
– Ujwal Neupane
Nov 20 at 4:38
@ChetanRanpariya is there any way to get value 108.2900 only from above data? That would work for me.
– Ujwal Neupane
Nov 20 at 4:43