Using VS 2019, Dot Net Core, Azure Function. HttpTrigger.
I've worked with JSON files previously in .Net Desktop and Server app dev. So, don't know what's happening in Azure and with Newtonsoft. When I try and dump the value of wht.YourName.last, I get the error,
Object reference not set to an instance of an object
which confuses me because in my other programming with System.Text.Json
, if I have a class model for a JSON file structure that has a child, the usual function call I use, which is JsonSerializer.Deserialize<jsonclass>(jsonstring)
, it has no problem setting up the child of the parent parent (reference below, class structure).
Is there a difference between the two function implementations, JsonConvert.DeserializeObject
and JsonSerializer.Deserialize
?
All of the code samples I've seen seem for Newtonsoft's method seem straight forward as its counterpart in dot net, which doesn't require me to have to do any pre-initalization of the NameBlock class/object.
Hope I explained that well enough considering my brain is kind of fried learning Azure.
Here is my deserialization code using Newtonsoft's method:
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
WebhookTest wht = JsonConvert.DeserializeObject<WebhookTest>(requestBody);
Raw dump of httprequest.Body:
{
"FormID":"1081011",
"UniqueID":"178165183",
"support_request":"",
"your_name":{
"first":"Testy",
"last":"Tester"
},
"email":"jxxxx@gxxxcxxxaxxxxxxxx.com",
"phone":"(111) 867-5309",
"upload_a_file_optional":"",
"request_details":"Testing latest revision of Azure function that deserializes JSON data into a class object."
}
This is what my class structure looks like:
public class NameBlock
{
public string first { get; set; }
public string last { get; set; }
}
public class WebhookTest
{
public string FormID { get; set; }
public string UniqueID { get; set; }
public string SupportRequest { get; set; }
public NameBlock YourName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
[JsonProperty("Uploadafile(optional)")]
public string UploadafileOptional { get; set; }
public string RequestDetails { get; set; }
}