I am aware this topic has been previously posted. But I tried following them. However, my result is still not being shown. I would appreciate any help possible. Thanks in advance. :) I am getting the following error: System.NullReferenceException: 'Object reference not set to an instance of an object.'
I am trying to deserialize a JSON object into a c# object to output the property score.
My Json output from json = toneAnalyzer.Tone(toneInput) :
{
"document_tone" : {
"tones" :
[
{
"score" : 0.70123,
"tone_id" : "tentative",
"tone_name" : "Tentative"
}
]
}
}
I have carried out the following code:
var json = toneAnalyzer.Tone(toneInput); // this is my json source
DocTone myResult = new DocTone();
myResult = JsonConvert.DeserializeObject<DocTone>(json.Response);
foreach (var myTone in myResult.tones)
{
Console.Write(myTone.Score);
Console.ReadKey();
}
// Console.WriteLine(myResult);
// Console.WriteLine(result.Response);
}
public class MyTone1
{
[JsonProperty("score")]
public double Score { get; set; }
[JsonProperty("tone_id")]
public string Tone_Id { get; set; }
[JsonProperty("tone_name")]
public string Tone_Name { get; set; }
}
public class DocTone
{
[JsonProperty("tones")]
public List<MyTone1> tones { get; set; }
}
>(json.Response);
– auburg Feb 21 '20 at 11:48