JsonDeserialize is returning null values after attempting to bind to model
I am attempting to link values from sample.json to a model called PayInfo. I am using Newtonsoft JsonDeserialize and though it is
a) locating the correct path (sample.json);and
b) accessing my models correctly,
it is not linking each value of the sample.json data to the model. When debugging, the fields display null in their values even though the sample.json file has plenty of dummy data in there. Please note that my goal is to deserialize the data from sample.json, bind it to model, and display it on my view.
Any tips would be highly appreciated!!
JsonSerializeMethod
public static List<PayInfo> ReadJson()
{
// read file into a string and deserialize JSON to a type
var payInfoData1 = JsonConvert.DeserializeObject<List<PayInfo>>(File.ReadAllText(@"JsonDatasample.json"));
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"JsonDatasample.json"))
{
JsonSerializer serializer = new JsonSerializer();
// PayInfo payInfoData = (PayInfo)serializer.Deserialize(file, typeof(PayInfo));
payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
return payInfoData1;
}
}
Model
public class PayInfo
{
public Payee Payee { get; set; }
public Payment Payment { get; set; }
public List<Remittance> Remittance { get; set; }
}
public class Payee
{
[JsonProperty("Name")]
public string PayeeName { get; set; }
[JsonProperty("Fax")]
public string PayeeFax { get; set; }
[JsonProperty("Phone")]
public string PayeePhone { get; set; }
public Address Address { get; set; }
public string Attention { get; set; }
public string SubmissionDate { get; set; }
public string PaymentExp { get; set; }
}
public class Payment
{
public string PAN { get; set; }
public string CVV { get; set; }
public string Exp { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string StateOrProvince { get; set; }
public string Zip { get; set; }
}
public class Remittance
{
public string PayorName { get; set; }
public string PayorId { get; set; }
public string InvoiceNo { get; set; }
public string Description { get; set; }
public string Amount { get; set; }
}
*Here is my controller
public class HomeController : Controller
{
// GET: /Home/
[HttpGet]
[Route("")]
public IActionResult Index()
{
// PayInfo payData = JsonToFile<PayInfo>.ReadJson();
// return View(payData);
PayInfo payList = new PayInfo();
var wordUp = JsonToFile<payList>.ReadJson();
return View(payList);
}
}
JsonData from sample.json
[
{
"Payee": {
"Name": "BLEENDOT",
"Fax": "(942) 424-2678",
"Phone": "(980) 494-2960",
"Address": {
"Address1": "551 Hoyt Street",
"Address2": "",
"City": "Rivera",
"StateOrProvince": "Ohio",
"Country": "US",
"PostalCode": 40529
},
"Attention": "Mcdaniel Blankenship",
"SubmissionDate": "2017-02-06"
},
"Payment": {
"PAN": 1313027774141142,
"CVV": 723,
"Exp": "11/2017"
},
"Remittance": [
{
"PayorName": "Cubix",
"PayorId": 8314,
"InvoiceNo": 16981,
"Description": "Aliquip et aliqua nisi sit sit sint voluptate exercitation quis dolore aute tempor mollit fugiat.",
"Amount": "$28,192.35"
},
{
"PayorName": "Oceanica",
"PayorId": 6013,
"InvoiceNo": 930,
"Description": "Cillum est est aute aliquip magna occaecat eiusmod labore velit consequat aute occaecat non eu.",
"Amount": "$76,664.75"
},
{
"PayorName": "Biotica",
"PayorId": 18461,
"InvoiceNo": 542,
"Description": "Exercitation minim ex sint velit amet.",
"Amount": "$30,718.78"
}
]
}
]
c# asp.net json serialization json.net
add a comment |
I am attempting to link values from sample.json to a model called PayInfo. I am using Newtonsoft JsonDeserialize and though it is
a) locating the correct path (sample.json);and
b) accessing my models correctly,
it is not linking each value of the sample.json data to the model. When debugging, the fields display null in their values even though the sample.json file has plenty of dummy data in there. Please note that my goal is to deserialize the data from sample.json, bind it to model, and display it on my view.
Any tips would be highly appreciated!!
JsonSerializeMethod
public static List<PayInfo> ReadJson()
{
// read file into a string and deserialize JSON to a type
var payInfoData1 = JsonConvert.DeserializeObject<List<PayInfo>>(File.ReadAllText(@"JsonDatasample.json"));
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"JsonDatasample.json"))
{
JsonSerializer serializer = new JsonSerializer();
// PayInfo payInfoData = (PayInfo)serializer.Deserialize(file, typeof(PayInfo));
payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
return payInfoData1;
}
}
Model
public class PayInfo
{
public Payee Payee { get; set; }
public Payment Payment { get; set; }
public List<Remittance> Remittance { get; set; }
}
public class Payee
{
[JsonProperty("Name")]
public string PayeeName { get; set; }
[JsonProperty("Fax")]
public string PayeeFax { get; set; }
[JsonProperty("Phone")]
public string PayeePhone { get; set; }
public Address Address { get; set; }
public string Attention { get; set; }
public string SubmissionDate { get; set; }
public string PaymentExp { get; set; }
}
public class Payment
{
public string PAN { get; set; }
public string CVV { get; set; }
public string Exp { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string StateOrProvince { get; set; }
public string Zip { get; set; }
}
public class Remittance
{
public string PayorName { get; set; }
public string PayorId { get; set; }
public string InvoiceNo { get; set; }
public string Description { get; set; }
public string Amount { get; set; }
}
*Here is my controller
public class HomeController : Controller
{
// GET: /Home/
[HttpGet]
[Route("")]
public IActionResult Index()
{
// PayInfo payData = JsonToFile<PayInfo>.ReadJson();
// return View(payData);
PayInfo payList = new PayInfo();
var wordUp = JsonToFile<payList>.ReadJson();
return View(payList);
}
}
JsonData from sample.json
[
{
"Payee": {
"Name": "BLEENDOT",
"Fax": "(942) 424-2678",
"Phone": "(980) 494-2960",
"Address": {
"Address1": "551 Hoyt Street",
"Address2": "",
"City": "Rivera",
"StateOrProvince": "Ohio",
"Country": "US",
"PostalCode": 40529
},
"Attention": "Mcdaniel Blankenship",
"SubmissionDate": "2017-02-06"
},
"Payment": {
"PAN": 1313027774141142,
"CVV": 723,
"Exp": "11/2017"
},
"Remittance": [
{
"PayorName": "Cubix",
"PayorId": 8314,
"InvoiceNo": 16981,
"Description": "Aliquip et aliqua nisi sit sit sint voluptate exercitation quis dolore aute tempor mollit fugiat.",
"Amount": "$28,192.35"
},
{
"PayorName": "Oceanica",
"PayorId": 6013,
"InvoiceNo": 930,
"Description": "Cillum est est aute aliquip magna occaecat eiusmod labore velit consequat aute occaecat non eu.",
"Amount": "$76,664.75"
},
{
"PayorName": "Biotica",
"PayorId": 18461,
"InvoiceNo": 542,
"Description": "Exercitation minim ex sint velit amet.",
"Amount": "$30,718.78"
}
]
}
]
c# asp.net json serialization json.net
2
Hi, welcome to SO. for anyone to help you get proper answer, you need to add code and related info. What you have provided is not sufficient. Please follow this guidelines and update your question. https://stackoverflow.com/help/mcve
– dj79
Oct 30 '18 at 17:32
1
Can you also attach the sample json file? Also try(IEnumerable<PayInfo>)serializer.Deserialize(file, typeof(IEnumerable<PayInfo>));
– Mohsin Mehmood
Oct 30 '18 at 18:30
1
Hello, and welcome to stackoverflow. Might you please edit your question to include your JSON as well as your code as text rather than as a screenshot? It's policy here not to to use images for textual data, see Discourage screenshots of code and/or errors and Why not upload images of code on SO when asking a question for why. A Minimal, Complete, and Verifiable example would increase your chances of getting an answer, for why see How to Ask.
– dbc
Oct 30 '18 at 19:35
Edited post to include sample.json code as well in text. Please let me know what you think :)
– nhmishaq
Nov 1 '18 at 7:26
Hey @nzrytmn, I've updated the code with your suggestions; now my question is how would I send a serialized object of this payInfoData1 in the controller to the view and unpack it there on the frontend?
– nhmishaq
Nov 14 '18 at 11:33
add a comment |
I am attempting to link values from sample.json to a model called PayInfo. I am using Newtonsoft JsonDeserialize and though it is
a) locating the correct path (sample.json);and
b) accessing my models correctly,
it is not linking each value of the sample.json data to the model. When debugging, the fields display null in their values even though the sample.json file has plenty of dummy data in there. Please note that my goal is to deserialize the data from sample.json, bind it to model, and display it on my view.
Any tips would be highly appreciated!!
JsonSerializeMethod
public static List<PayInfo> ReadJson()
{
// read file into a string and deserialize JSON to a type
var payInfoData1 = JsonConvert.DeserializeObject<List<PayInfo>>(File.ReadAllText(@"JsonDatasample.json"));
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"JsonDatasample.json"))
{
JsonSerializer serializer = new JsonSerializer();
// PayInfo payInfoData = (PayInfo)serializer.Deserialize(file, typeof(PayInfo));
payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
return payInfoData1;
}
}
Model
public class PayInfo
{
public Payee Payee { get; set; }
public Payment Payment { get; set; }
public List<Remittance> Remittance { get; set; }
}
public class Payee
{
[JsonProperty("Name")]
public string PayeeName { get; set; }
[JsonProperty("Fax")]
public string PayeeFax { get; set; }
[JsonProperty("Phone")]
public string PayeePhone { get; set; }
public Address Address { get; set; }
public string Attention { get; set; }
public string SubmissionDate { get; set; }
public string PaymentExp { get; set; }
}
public class Payment
{
public string PAN { get; set; }
public string CVV { get; set; }
public string Exp { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string StateOrProvince { get; set; }
public string Zip { get; set; }
}
public class Remittance
{
public string PayorName { get; set; }
public string PayorId { get; set; }
public string InvoiceNo { get; set; }
public string Description { get; set; }
public string Amount { get; set; }
}
*Here is my controller
public class HomeController : Controller
{
// GET: /Home/
[HttpGet]
[Route("")]
public IActionResult Index()
{
// PayInfo payData = JsonToFile<PayInfo>.ReadJson();
// return View(payData);
PayInfo payList = new PayInfo();
var wordUp = JsonToFile<payList>.ReadJson();
return View(payList);
}
}
JsonData from sample.json
[
{
"Payee": {
"Name": "BLEENDOT",
"Fax": "(942) 424-2678",
"Phone": "(980) 494-2960",
"Address": {
"Address1": "551 Hoyt Street",
"Address2": "",
"City": "Rivera",
"StateOrProvince": "Ohio",
"Country": "US",
"PostalCode": 40529
},
"Attention": "Mcdaniel Blankenship",
"SubmissionDate": "2017-02-06"
},
"Payment": {
"PAN": 1313027774141142,
"CVV": 723,
"Exp": "11/2017"
},
"Remittance": [
{
"PayorName": "Cubix",
"PayorId": 8314,
"InvoiceNo": 16981,
"Description": "Aliquip et aliqua nisi sit sit sint voluptate exercitation quis dolore aute tempor mollit fugiat.",
"Amount": "$28,192.35"
},
{
"PayorName": "Oceanica",
"PayorId": 6013,
"InvoiceNo": 930,
"Description": "Cillum est est aute aliquip magna occaecat eiusmod labore velit consequat aute occaecat non eu.",
"Amount": "$76,664.75"
},
{
"PayorName": "Biotica",
"PayorId": 18461,
"InvoiceNo": 542,
"Description": "Exercitation minim ex sint velit amet.",
"Amount": "$30,718.78"
}
]
}
]
c# asp.net json serialization json.net
I am attempting to link values from sample.json to a model called PayInfo. I am using Newtonsoft JsonDeserialize and though it is
a) locating the correct path (sample.json);and
b) accessing my models correctly,
it is not linking each value of the sample.json data to the model. When debugging, the fields display null in their values even though the sample.json file has plenty of dummy data in there. Please note that my goal is to deserialize the data from sample.json, bind it to model, and display it on my view.
Any tips would be highly appreciated!!
JsonSerializeMethod
public static List<PayInfo> ReadJson()
{
// read file into a string and deserialize JSON to a type
var payInfoData1 = JsonConvert.DeserializeObject<List<PayInfo>>(File.ReadAllText(@"JsonDatasample.json"));
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"JsonDatasample.json"))
{
JsonSerializer serializer = new JsonSerializer();
// PayInfo payInfoData = (PayInfo)serializer.Deserialize(file, typeof(PayInfo));
payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
return payInfoData1;
}
}
Model
public class PayInfo
{
public Payee Payee { get; set; }
public Payment Payment { get; set; }
public List<Remittance> Remittance { get; set; }
}
public class Payee
{
[JsonProperty("Name")]
public string PayeeName { get; set; }
[JsonProperty("Fax")]
public string PayeeFax { get; set; }
[JsonProperty("Phone")]
public string PayeePhone { get; set; }
public Address Address { get; set; }
public string Attention { get; set; }
public string SubmissionDate { get; set; }
public string PaymentExp { get; set; }
}
public class Payment
{
public string PAN { get; set; }
public string CVV { get; set; }
public string Exp { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string StateOrProvince { get; set; }
public string Zip { get; set; }
}
public class Remittance
{
public string PayorName { get; set; }
public string PayorId { get; set; }
public string InvoiceNo { get; set; }
public string Description { get; set; }
public string Amount { get; set; }
}
*Here is my controller
public class HomeController : Controller
{
// GET: /Home/
[HttpGet]
[Route("")]
public IActionResult Index()
{
// PayInfo payData = JsonToFile<PayInfo>.ReadJson();
// return View(payData);
PayInfo payList = new PayInfo();
var wordUp = JsonToFile<payList>.ReadJson();
return View(payList);
}
}
JsonData from sample.json
[
{
"Payee": {
"Name": "BLEENDOT",
"Fax": "(942) 424-2678",
"Phone": "(980) 494-2960",
"Address": {
"Address1": "551 Hoyt Street",
"Address2": "",
"City": "Rivera",
"StateOrProvince": "Ohio",
"Country": "US",
"PostalCode": 40529
},
"Attention": "Mcdaniel Blankenship",
"SubmissionDate": "2017-02-06"
},
"Payment": {
"PAN": 1313027774141142,
"CVV": 723,
"Exp": "11/2017"
},
"Remittance": [
{
"PayorName": "Cubix",
"PayorId": 8314,
"InvoiceNo": 16981,
"Description": "Aliquip et aliqua nisi sit sit sint voluptate exercitation quis dolore aute tempor mollit fugiat.",
"Amount": "$28,192.35"
},
{
"PayorName": "Oceanica",
"PayorId": 6013,
"InvoiceNo": 930,
"Description": "Cillum est est aute aliquip magna occaecat eiusmod labore velit consequat aute occaecat non eu.",
"Amount": "$76,664.75"
},
{
"PayorName": "Biotica",
"PayorId": 18461,
"InvoiceNo": 542,
"Description": "Exercitation minim ex sint velit amet.",
"Amount": "$30,718.78"
}
]
}
]
c# asp.net json serialization json.net
c# asp.net json serialization json.net
edited Nov 14 '18 at 11:42
nhmishaq
asked Oct 30 '18 at 17:19
nhmishaqnhmishaq
93
93
2
Hi, welcome to SO. for anyone to help you get proper answer, you need to add code and related info. What you have provided is not sufficient. Please follow this guidelines and update your question. https://stackoverflow.com/help/mcve
– dj79
Oct 30 '18 at 17:32
1
Can you also attach the sample json file? Also try(IEnumerable<PayInfo>)serializer.Deserialize(file, typeof(IEnumerable<PayInfo>));
– Mohsin Mehmood
Oct 30 '18 at 18:30
1
Hello, and welcome to stackoverflow. Might you please edit your question to include your JSON as well as your code as text rather than as a screenshot? It's policy here not to to use images for textual data, see Discourage screenshots of code and/or errors and Why not upload images of code on SO when asking a question for why. A Minimal, Complete, and Verifiable example would increase your chances of getting an answer, for why see How to Ask.
– dbc
Oct 30 '18 at 19:35
Edited post to include sample.json code as well in text. Please let me know what you think :)
– nhmishaq
Nov 1 '18 at 7:26
Hey @nzrytmn, I've updated the code with your suggestions; now my question is how would I send a serialized object of this payInfoData1 in the controller to the view and unpack it there on the frontend?
– nhmishaq
Nov 14 '18 at 11:33
add a comment |
2
Hi, welcome to SO. for anyone to help you get proper answer, you need to add code and related info. What you have provided is not sufficient. Please follow this guidelines and update your question. https://stackoverflow.com/help/mcve
– dj79
Oct 30 '18 at 17:32
1
Can you also attach the sample json file? Also try(IEnumerable<PayInfo>)serializer.Deserialize(file, typeof(IEnumerable<PayInfo>));
– Mohsin Mehmood
Oct 30 '18 at 18:30
1
Hello, and welcome to stackoverflow. Might you please edit your question to include your JSON as well as your code as text rather than as a screenshot? It's policy here not to to use images for textual data, see Discourage screenshots of code and/or errors and Why not upload images of code on SO when asking a question for why. A Minimal, Complete, and Verifiable example would increase your chances of getting an answer, for why see How to Ask.
– dbc
Oct 30 '18 at 19:35
Edited post to include sample.json code as well in text. Please let me know what you think :)
– nhmishaq
Nov 1 '18 at 7:26
Hey @nzrytmn, I've updated the code with your suggestions; now my question is how would I send a serialized object of this payInfoData1 in the controller to the view and unpack it there on the frontend?
– nhmishaq
Nov 14 '18 at 11:33
2
2
Hi, welcome to SO. for anyone to help you get proper answer, you need to add code and related info. What you have provided is not sufficient. Please follow this guidelines and update your question. https://stackoverflow.com/help/mcve
– dj79
Oct 30 '18 at 17:32
Hi, welcome to SO. for anyone to help you get proper answer, you need to add code and related info. What you have provided is not sufficient. Please follow this guidelines and update your question. https://stackoverflow.com/help/mcve
– dj79
Oct 30 '18 at 17:32
1
1
Can you also attach the sample json file? Also try
(IEnumerable<PayInfo>)serializer.Deserialize(file, typeof(IEnumerable<PayInfo>));
– Mohsin Mehmood
Oct 30 '18 at 18:30
Can you also attach the sample json file? Also try
(IEnumerable<PayInfo>)serializer.Deserialize(file, typeof(IEnumerable<PayInfo>));
– Mohsin Mehmood
Oct 30 '18 at 18:30
1
1
Hello, and welcome to stackoverflow. Might you please edit your question to include your JSON as well as your code as text rather than as a screenshot? It's policy here not to to use images for textual data, see Discourage screenshots of code and/or errors and Why not upload images of code on SO when asking a question for why. A Minimal, Complete, and Verifiable example would increase your chances of getting an answer, for why see How to Ask.
– dbc
Oct 30 '18 at 19:35
Hello, and welcome to stackoverflow. Might you please edit your question to include your JSON as well as your code as text rather than as a screenshot? It's policy here not to to use images for textual data, see Discourage screenshots of code and/or errors and Why not upload images of code on SO when asking a question for why. A Minimal, Complete, and Verifiable example would increase your chances of getting an answer, for why see How to Ask.
– dbc
Oct 30 '18 at 19:35
Edited post to include sample.json code as well in text. Please let me know what you think :)
– nhmishaq
Nov 1 '18 at 7:26
Edited post to include sample.json code as well in text. Please let me know what you think :)
– nhmishaq
Nov 1 '18 at 7:26
Hey @nzrytmn, I've updated the code with your suggestions; now my question is how would I send a serialized object of this payInfoData1 in the controller to the view and unpack it there on the frontend?
– nhmishaq
Nov 14 '18 at 11:33
Hey @nzrytmn, I've updated the code with your suggestions; now my question is how would I send a serialized object of this payInfoData1 in the controller to the view and unpack it there on the frontend?
– nhmishaq
Nov 14 '18 at 11:33
add a comment |
1 Answer
1
active
oldest
votes
First, the name of your properties are not correct with json names; they should be same or you have to use a jsonProperty attribute. And the main class name is not correct; your main class is should be a class that includes your sub class like 'Payee', 'Address' and rest , please see my codes, i fixed a bit.
public class PayInfo
{
public Payee Payee { get; set; }
public Payment Payment { get; set; }
public List<Remittance> Remittance { get; set; }
}
public class Payee
{
[JsonProperty("Name")]
public string PayeeName { get; set; }
[JsonProperty("Fax")]
public string PayeeFax { get; set; }
[JsonProperty("Phone")]
public string PayeePhone { get; set; }
public Address Address { get; set; }
public string Attention { get; set; }
public string SubmissionDate { get; set; }
public string PaymentExp { get; set; }
}
public class Payment
{
public string PAN { get; set; }
public string CVV { get; set; }
public string Exp { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string StateOrProvince { get; set; }
public string Zip { get; set; }
}
public class Remittance
{
public string PayorName { get; set; }
public string PayorId { get; set; }
public string InvoiceNo { get; set; }
public string Description { get; set; }
public string Amount { get; set; }
}
And you can modify your method like this as using list instead of array , will be easier.
public static List<PayInfo> ReadJson()
{
// read file into a string and deserialize JSON to a type
var payInfoData1 = JsonConvert.DeserializeObject<List<PayInfo>>(File.ReadAllText(@"JsonDatasample.json"));
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"JsonDatasample.json"))
{
JsonSerializer serializer = new JsonSerializer();
// PayInfo payInfoData = (PayInfo)serializer.Deserialize(file, typeof(PayInfo));
payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
return payInfoData1;
}
}
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%2f53069663%2fjsondeserialize-is-returning-null-values-after-attempting-to-bind-to-model%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
First, the name of your properties are not correct with json names; they should be same or you have to use a jsonProperty attribute. And the main class name is not correct; your main class is should be a class that includes your sub class like 'Payee', 'Address' and rest , please see my codes, i fixed a bit.
public class PayInfo
{
public Payee Payee { get; set; }
public Payment Payment { get; set; }
public List<Remittance> Remittance { get; set; }
}
public class Payee
{
[JsonProperty("Name")]
public string PayeeName { get; set; }
[JsonProperty("Fax")]
public string PayeeFax { get; set; }
[JsonProperty("Phone")]
public string PayeePhone { get; set; }
public Address Address { get; set; }
public string Attention { get; set; }
public string SubmissionDate { get; set; }
public string PaymentExp { get; set; }
}
public class Payment
{
public string PAN { get; set; }
public string CVV { get; set; }
public string Exp { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string StateOrProvince { get; set; }
public string Zip { get; set; }
}
public class Remittance
{
public string PayorName { get; set; }
public string PayorId { get; set; }
public string InvoiceNo { get; set; }
public string Description { get; set; }
public string Amount { get; set; }
}
And you can modify your method like this as using list instead of array , will be easier.
public static List<PayInfo> ReadJson()
{
// read file into a string and deserialize JSON to a type
var payInfoData1 = JsonConvert.DeserializeObject<List<PayInfo>>(File.ReadAllText(@"JsonDatasample.json"));
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"JsonDatasample.json"))
{
JsonSerializer serializer = new JsonSerializer();
// PayInfo payInfoData = (PayInfo)serializer.Deserialize(file, typeof(PayInfo));
payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
return payInfoData1;
}
}
add a comment |
First, the name of your properties are not correct with json names; they should be same or you have to use a jsonProperty attribute. And the main class name is not correct; your main class is should be a class that includes your sub class like 'Payee', 'Address' and rest , please see my codes, i fixed a bit.
public class PayInfo
{
public Payee Payee { get; set; }
public Payment Payment { get; set; }
public List<Remittance> Remittance { get; set; }
}
public class Payee
{
[JsonProperty("Name")]
public string PayeeName { get; set; }
[JsonProperty("Fax")]
public string PayeeFax { get; set; }
[JsonProperty("Phone")]
public string PayeePhone { get; set; }
public Address Address { get; set; }
public string Attention { get; set; }
public string SubmissionDate { get; set; }
public string PaymentExp { get; set; }
}
public class Payment
{
public string PAN { get; set; }
public string CVV { get; set; }
public string Exp { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string StateOrProvince { get; set; }
public string Zip { get; set; }
}
public class Remittance
{
public string PayorName { get; set; }
public string PayorId { get; set; }
public string InvoiceNo { get; set; }
public string Description { get; set; }
public string Amount { get; set; }
}
And you can modify your method like this as using list instead of array , will be easier.
public static List<PayInfo> ReadJson()
{
// read file into a string and deserialize JSON to a type
var payInfoData1 = JsonConvert.DeserializeObject<List<PayInfo>>(File.ReadAllText(@"JsonDatasample.json"));
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"JsonDatasample.json"))
{
JsonSerializer serializer = new JsonSerializer();
// PayInfo payInfoData = (PayInfo)serializer.Deserialize(file, typeof(PayInfo));
payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
return payInfoData1;
}
}
add a comment |
First, the name of your properties are not correct with json names; they should be same or you have to use a jsonProperty attribute. And the main class name is not correct; your main class is should be a class that includes your sub class like 'Payee', 'Address' and rest , please see my codes, i fixed a bit.
public class PayInfo
{
public Payee Payee { get; set; }
public Payment Payment { get; set; }
public List<Remittance> Remittance { get; set; }
}
public class Payee
{
[JsonProperty("Name")]
public string PayeeName { get; set; }
[JsonProperty("Fax")]
public string PayeeFax { get; set; }
[JsonProperty("Phone")]
public string PayeePhone { get; set; }
public Address Address { get; set; }
public string Attention { get; set; }
public string SubmissionDate { get; set; }
public string PaymentExp { get; set; }
}
public class Payment
{
public string PAN { get; set; }
public string CVV { get; set; }
public string Exp { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string StateOrProvince { get; set; }
public string Zip { get; set; }
}
public class Remittance
{
public string PayorName { get; set; }
public string PayorId { get; set; }
public string InvoiceNo { get; set; }
public string Description { get; set; }
public string Amount { get; set; }
}
And you can modify your method like this as using list instead of array , will be easier.
public static List<PayInfo> ReadJson()
{
// read file into a string and deserialize JSON to a type
var payInfoData1 = JsonConvert.DeserializeObject<List<PayInfo>>(File.ReadAllText(@"JsonDatasample.json"));
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"JsonDatasample.json"))
{
JsonSerializer serializer = new JsonSerializer();
// PayInfo payInfoData = (PayInfo)serializer.Deserialize(file, typeof(PayInfo));
payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
return payInfoData1;
}
}
First, the name of your properties are not correct with json names; they should be same or you have to use a jsonProperty attribute. And the main class name is not correct; your main class is should be a class that includes your sub class like 'Payee', 'Address' and rest , please see my codes, i fixed a bit.
public class PayInfo
{
public Payee Payee { get; set; }
public Payment Payment { get; set; }
public List<Remittance> Remittance { get; set; }
}
public class Payee
{
[JsonProperty("Name")]
public string PayeeName { get; set; }
[JsonProperty("Fax")]
public string PayeeFax { get; set; }
[JsonProperty("Phone")]
public string PayeePhone { get; set; }
public Address Address { get; set; }
public string Attention { get; set; }
public string SubmissionDate { get; set; }
public string PaymentExp { get; set; }
}
public class Payment
{
public string PAN { get; set; }
public string CVV { get; set; }
public string Exp { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string StateOrProvince { get; set; }
public string Zip { get; set; }
}
public class Remittance
{
public string PayorName { get; set; }
public string PayorId { get; set; }
public string InvoiceNo { get; set; }
public string Description { get; set; }
public string Amount { get; set; }
}
And you can modify your method like this as using list instead of array , will be easier.
public static List<PayInfo> ReadJson()
{
// read file into a string and deserialize JSON to a type
var payInfoData1 = JsonConvert.DeserializeObject<List<PayInfo>>(File.ReadAllText(@"JsonDatasample.json"));
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"JsonDatasample.json"))
{
JsonSerializer serializer = new JsonSerializer();
// PayInfo payInfoData = (PayInfo)serializer.Deserialize(file, typeof(PayInfo));
payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
return payInfoData1;
}
}
edited Nov 1 '18 at 9:40
answered Nov 1 '18 at 9:32
nzrytmnnzrytmn
561410
561410
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%2f53069663%2fjsondeserialize-is-returning-null-values-after-attempting-to-bind-to-model%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
2
Hi, welcome to SO. for anyone to help you get proper answer, you need to add code and related info. What you have provided is not sufficient. Please follow this guidelines and update your question. https://stackoverflow.com/help/mcve
– dj79
Oct 30 '18 at 17:32
1
Can you also attach the sample json file? Also try
(IEnumerable<PayInfo>)serializer.Deserialize(file, typeof(IEnumerable<PayInfo>));
– Mohsin Mehmood
Oct 30 '18 at 18:30
1
Hello, and welcome to stackoverflow. Might you please edit your question to include your JSON as well as your code as text rather than as a screenshot? It's policy here not to to use images for textual data, see Discourage screenshots of code and/or errors and Why not upload images of code on SO when asking a question for why. A Minimal, Complete, and Verifiable example would increase your chances of getting an answer, for why see How to Ask.
– dbc
Oct 30 '18 at 19:35
Edited post to include sample.json code as well in text. Please let me know what you think :)
– nhmishaq
Nov 1 '18 at 7:26
Hey @nzrytmn, I've updated the code with your suggestions; now my question is how would I send a serialized object of this payInfoData1 in the controller to the view and unpack it there on the frontend?
– nhmishaq
Nov 14 '18 at 11:33