Home > Software design >  json is parsed as null
json is parsed as null

Time:01-26

I have a json structure like below.

{"data":{"accountType":"New"}}

The model for this is as follows.

 public class Data
 {
     public string accountType { get; set; }
 }

Below is how I am parsing this data using System.Text.Json.

string json2 = "{\"data\":{\"accountType\":\"New\"}}\n";
var account = JsonSerializer.Deserialize<Data>(json2);

but account is parsed as null. What am I doing wrong here?

CodePudding user response:

you need a root class

public class Root
 {
     public Data data { get; set; }
 }
public class Data
 {
     public string accountType { get; set; }
 }

and code

var account = JsonSerializer.Deserialize<Root>(json2);
  •  Tags:  
  • Related