Home > Software design >  Newtonsoft.Json returns incorrect values from file (with DeserializeObject)
Newtonsoft.Json returns incorrect values from file (with DeserializeObject)

Time:07-26

I use json.net to get the values from the file and output them to the console. Here is the json file:

[
  {
    "id": 0,
    "text": "aaa",
    "speaker": "mike",
    "next_node": 555,
    "attached_script": "script"
  },

  {
    "id": 1,
    "text": "bbb",
    "speaker": "tom",
    "next_node": 2,
    "attached_script": ""
  }
]

Here is my main code (I use a separate class to get all the values of the corresponding structure):

public class PhraseNode
    {
        
        public int id { get; set; }
        public string text { get; set; }
        public string speaker { get; set; }
        public int nextnode { get; set; }
        public string addscript { get; set; }

    }
    internal class Program
    {
        static void RunDialog(string path)
        {
            using (StreamReader sr = new StreamReader(path))
            {
                string json = sr.ReadToEnd();
                List<PhraseNode> nodes = JsonConvert.DeserializeObject<List<PhraseNode>>(json);
                Console.WriteLine("Кол-во фраз: "   nodes.Count);

                Console.Write(nodes[0].speaker   ": ");
                Console.WriteLine(nodes[0].text);
                Console.WriteLine(nodes[0].nextnode.ToString());
                Console.WriteLine(nodes[0].addscript);
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            RunDialog("D:\\json1.json");
            Console.WriteLine("press any key...");
            Console.ReadKey();
        }
    }

The "speaker" and "text" strings are output normally, but instead of the "nextnode" value (555), 0 is output, and an empty string is output instead of "addscript". What's the problem here? I will be grateful for any help.

CodePudding user response:

Serialize can't match the name json properties names next_node and attached_script to the corresponding class properties. You can use JsonPropertyAttribute to help it:

public class PhraseNode
{
    public int id { get; set; }
    public string text { get; set; }
    public string speaker { get; set; }
    [JsonProperty("next_node")]
    public int nextnode { get; set; }
    [JsonProperty("attached_script")]
    public string addscript { get; set; }
}

Or provide the naming policy to the serializer:

DefaultContractResolver contractResolver = new DefaultContractResolver
{
    NamingStrategy = new SnakeCaseNamingStrategy()
};

List<PhraseNode> nodes = JsonConvert.DeserializeObject<List<PhraseNode>>(json, new JsonSerializerSettings
{
    ContractResolver = contractResolver,
    Formatting = Formatting.Indented
});

P.S. I recommend using standard naming conventions so make properties names pascal case.

CodePudding user response:

your json properties and c# class properties should be the same. I recommend to use JsonProperty attributes

 public partial class PhraseNode
    {
        [JsonProperty("id")]
        public long Id { get; set; }

        [JsonProperty("text")]
        public string Text { get; set; }

        [JsonProperty("speaker")]
        public string Speaker { get; set; }

        [JsonProperty("next_node")]
        public long NextNode { get; set; }

        [JsonProperty("attached_script")]
        public string AttachedScript { get; set; }
    }
  • Related