Reputation: 21
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(@"JsonData\sample.json"));
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"JsonData\sample.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"
}
]
}
]
Upvotes: 0
Views: 796
Reputation: 6941
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(@"JsonData\sample.json"));
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"JsonData\sample.json"))
{
JsonSerializer serializer = new JsonSerializer();
// PayInfo[] payInfoData = (PayInfo[])serializer.Deserialize(file, typeof(PayInfo[]));
payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
return payInfoData1;
}
}
Upvotes: 1