Search code examples
c#jsonjson-deserializationsystem.text.json

C# deserializes into 0/null?


I'm trying to learn deserialization and I have it semi-working, but when I try to deserialize an int, it returns 0 or null (no null errors just outputs 0).

public partial class Form1 : Form
{
    public string basicjson = Application.StartupPath + @"\testjsons\basicjson.json";
    public string nestedjson = Application.StartupPath + @"\testjsons\nestedjson.json";

    public Form1()
    {
        InitializeComponent();
    }

    public class basictest
    {
        public string Name { get; set; }
        public int contact { get; set; }
    }

    public class nestedtest
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public int age { get; set; }
        public address Address { get; set; }
        public phoneNumbers PhoneNumbers { get; set; }
    }

    public class address
    {
        public string streetAddress { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string postalCode { get; set; }
    }
    public class phoneNumbers
    {
        public string type { get; set; }
        public string number { get; set; }
    }

    private void serial_Click(object sender, EventArgs e)
    {


    }

    private void deserial_Click(object sender, EventArgs e)
    {
        string jsonString = File.ReadAllText(basicjson);
        basictest BasicJson = JsonSerializer.Deserialize<basictest>(jsonString);
        label1.Text = BasicJson.contact.ToString();
        listBox1.Items.Add(BasicJson.contact);
        textBox1.Text = BasicJson.contact.ToString();
    }

    private void deserialnested_Click(object sender, EventArgs e)
    {
        string jsonString = File.ReadAllText(nestedjson);
        nestedtest nestedJson = JsonSerializer.Deserialize<nestedtest>(jsonString);
        //label1.Text = nestedJson.PhoneNumbers.type;
        listBox1.Items.Add(nestedJson.PhoneNumbers.type);
        //textBox1.Text = nestedJson.PhoneNumbers.type;
    }
}
{
  "Name":"Denu",
  "Contact":12345678
}

{
    "firstName": "Rack",
    "lastName": "Jackon",
    "gender": "man",
    "age": 24,
    "address": {
        "streetAddress": "126",
        "city": "San Jone",
        "state": "CA",
        "postalCode": "394221"
    },
    "phoneNumbers": [
        { "type": "home", "number": "7383627627" }
    ]
}

I have to be missing something simple, but I've little experience.


Solution

  • you have to make uper case Contact property since in your json it is upercase

    BasicTest basicJson = JsonSerializer.Deserialize<BasicTest>(jsonString);
    
    public class BasicTest
        {
            public string Name { get; set; }
            public int Contact { get; set; }
        }
    

    and fix a nested test

     public address address { get; set; }
     public List<phoneNumbers> phoneNumbers { get; set; }
    
    
    label1.Text = nestedJson.phoneNumbers[0].type;