-1

Could be one of those days where I can't see what is infront of me but when I try to use the jsonserializer Populate function to map to an object - it fails to do so, and I'm not sure why. the object returned just has the default values for everything. Condensed code:



   string json = @"
            {   
                ""Notification"": {
                      ""Id"": 4,
                      ""CreatedById"": 1,
                      ""CreatedBy"": null,
                      ""CreatedDateTime"": ""2023-05-31T21:44:44.9659592+01:00"",
                      ""PushMessage"": ""Notify me now"",
                      ""ScheduledDateTime"": ""2023-05-31T22:46:00+01:00"",
                      ""Reach"": 1,
                      ""OrganizationId"": 1,                      
                      ""Organization"": {
                        ""Id"": 1,
                        ""Name"": ""orgname""
                        },
                      ""UserNotifications"": [],
                      ""Value"": 1,
                      ""DisplayName"": ""OrgNotification""
                      }
            }"; 
        
        JObject jo = JObject.Parse(json);
                
        var target = new Notification();
        serializer.Populate(jo.CreateReader(), target);



public class Notification{
    
    private string displayname;
    private int _value;
    
        public int Id { get; set; }
        public int CreatedById { get; set; }
        public User CreatedBy { get; set; }
        public DateTimeOffset CreatedDateTime { get; set; }
        public string PushMessage { get; set; }
        public DateTimeOffset? ScheduledDateTime { get; set; }
        public int Reach { get; set; }
        public int OrganizationId { get; set; }
        public Organization Organization { get; set; }  
        public string DisplayName { get{
            displayname = "OrgNotification";
            return displayname;} }
        public int Value { get{
            _value = 1;
            return _value;} }
}

public class Organization{
public int Id { get; set; } 
public string Name { get; set; }
}

1 Answers1

0

i would use JsonConvert.DeserializeObject(json); but for your question: it works as expected. you miss a wrapper class . this is example with works as you want. i added another class:


public class N { 
    public Notification Notification {get;set;}
}

the whole code:

string json = @"
            {   
                ""Notification"": {
                      ""Id"": 4,
                      ""CreatedById"": 1,
                      ""CreatedBy"": ""TEST"",
                      ""CreatedDateTime"": ""2023-05-31T21:44:44.9659592+01:00"",
                      ""PushMessage"": ""Notify me now"",
                      ""ScheduledDateTime"": ""2023-05-31T22:46:00+01:00"",
                      ""Reach"": 1,
                      ""OrganizationId"": 1,                      
                      ""Organization"": {
                        ""Id"": 1,
                        ""Name"": ""orgname""
                        },
                      ""UserNotifications"": [],
                      ""Value"": 1,
                      ""DisplayName"": ""OrgNotification""
                      }
            }";

    JObject jo = JObject.Parse(json);
    var target = new N(); // (!!!!)
    var serializer  = new JsonSerializer();
    serializer.Populate(jo.CreateReader(), target);
    Console.Write(target);

classes


public class N { 
    public Notification Notification {get;set;}
}

public class Notification
{

    private string displayname;
    private int _value;

    public int Id { get; set; }
    public int CreatedById { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedDateTime { get; set; }
    public string PushMessage { get; set; }
    public DateTime? ScheduledDateTime { get; set; }
    public int Reach { get; set; }
    public int OrganizationId { get; set; }
    public Organization Organization { get; set; }
    public string DisplayName
    {
        get
        {
            displayname = "OrgNotification";
            return displayname;
        }
    }
    public int Value
    {
        get
        {
            _value = 1;
            return _value;
        }
    }
    
    public Notification(){
        
    }
}

public class Organization
{
    public int Id { get; set; }
    public string Name { get; set; }
}


result enter image description here

Power Mouse
  • 727
  • 6
  • 16