-3

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
TheTripleDeuce
  • 103
  • 1
  • 1
  • 7
  • Does this answer your question? [System.Text.JSON doesn't deserialize what Newtonsoft does](https://stackoverflow.com/questions/58879190/system-text-json-doesnt-deserialize-what-newtonsoft-does) – gunr2171 Jun 30 '22 at 23:03

1 Answers1

0

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;
Serge
  • 40,935
  • 4
  • 18
  • 45
  • deserialize button spits this out still: jsonserializedeserialize.Form1.nestedtest.PhoneNumbers.get returned null. – TheTripleDeuce Jun 30 '22 at 23:08
  • 1
    @TheTripleDeuce You didn't mention nested test in your post – Serge Jun 30 '22 at 23:09
  • sorry yeah my bad im trying to learn nested and basic json structures – TheTripleDeuce Jun 30 '22 at 23:10
  • still throws: System.NullReferenceException: 'Object reference not set to an instance of an object.' jsonserializedeserialize.Form1.nestedtest.PhoneNumbers.get returned null. – TheTripleDeuce Jun 30 '22 at 23:19
  • @TheTripleDeuce What form1 has to do ? did you see my answer nestedJson.PhoneNumbers[0]? About Form1 post another question. This question is about json – Serge Jun 30 '22 at 23:40
  • label1.Text = nestedJson.PhoneNumbers[0].type; that is what i have that gives the error i just pasted what the error was – TheTripleDeuce Jun 30 '22 at 23:42
  • 1
    @TheTripleDeuce try to change to lower case public List phoneNumbers { get; set; } – Serge Jun 30 '22 at 23:46