-1

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; }
}
Peter B
  • 22,460
  • 5
  • 32
  • 69
Bobs
  • 9
  • 2
  • it's at the bottom of the code. Sorry I have never used this before. So not familiar with the format. – Bobs Feb 21 '20 at 11:46
  • You're deserializing a list so should be JsonConvert.DeserializeObject>(json.Response); – auburg Feb 21 '20 at 11:48
  • 3
    @auburg no, that's not correct. only `tones` **within** the JSON is a list. The outer structure is an object. I suspect the real issue is that the C# structure doesn't account for the `"document_tone"` property. – ADyson Feb 21 '20 at 11:53
  • Does var json even have a "Response" property? – Nuno Feb 21 '20 at 11:54
  • yes it does a have response property and it is returning the correct data. It is just the deserialize that is not working. – Bobs Feb 21 '20 at 11:55
  • 1
    I really dislike how a lot of SO questions are getting closed with "This question already has answers here" and a pretty much completely unrelated or outdated answer. :( The question has been correctly answered below and the answer has nothing to do with the linked question "What is a NullReferenceException, and how do I fix it?". – Longoon12000 Feb 21 '20 at 12:00
  • 1
    This question should not have been closed. It is not a duplicate of the referenced answer - the problem is caused by incorrect JSON deserialisation! It should be reopened. – Martin Feb 21 '20 at 12:01
  • https://dotnetfiddle.net/UcnNty - working demo of a correct structure to deserialise it. I recommend using a tool such as http://json2csharp.com/ to generate matching C# classes based on any given JSON sample. – ADyson Feb 21 '20 at 12:02
  • @ADyson. Thank you man. Thats cool. i did not knwo there was a tool that does it for you. Thank you so much. :) – Bobs Feb 21 '20 at 12:18

1 Answers1

2

You've got a slight mistake with the object you are deserialising to.

Your root object is not DocTone, but actually the object that has a property containing the DocTone object (via the document_tone element).

Define a root object (call it whatever you like) and then deserialise to that:

public class RootObject
{
    [JsonProperty("document_tone")]
    public DocTone DocTone { get; set; }
}

Deserialise and then access via the DocTone property:

RootObject myResult;
myResult = JsonConvert.DeserializeObject<RootObject>(json.Response);

foreach (var myTone in myResult.DocTone.tones)
    ...

The reason that you are experiencing the NullReferencException is because when you deserialise to a DocTone object, the tones property is NULL.

Martin
  • 16,093
  • 1
  • 29
  • 48
  • either that or, if possible, change the json to not include the `"document_tone" : {` and corresponding closing `}` – Longoon12000 Feb 21 '20 at 11:57
  • 1
    @Longoon12000 You are right that you could do that, but I fail to see why hacking the JSON would be better than correctly deserialising it. – Martin Feb 21 '20 at 12:00