-2

I'm doing a program and i need to interact with airtables api to get the registred members from there. But my code is not working, says that class.Id.get returned null. Code:

public class TraderData
{
    public string Id { get; set; }
    public string Fields { get; set; }
    public string Users {  get;  set; }
    public string Macaddress_value { get; set; }
    public string Email_value { get; set; }
    public string Createdtime { get; set; }
}

static void GetUserData()
{
    WebClient web = new WebClient();
    web.Headers.Set(HttpRequestHeader.Authorization, "Bearer keycxCsSzHzvAquck");
    string json = web.DownloadString("https://api.airtable.com/v0/appO1mkBuYFHD0tH5/users_control");
    var data = JsonConvert.DeserializeObject<TraderData>(json);
    Console.WriteLine(data.Id.ToString());
}

API:

{"records":[{"id":"reczOujwG3H1DuhR7","fields":{"Users":"user_juseh","macaddress_value":"123456789","email_value":"juseh@hotmail.com"}, "createdTime":"2018-04-21T22:36:48.000Z"}]}

dbc
  • 104,963
  • 20
  • 228
  • 340
SkyMixed
  • 13
  • 1

2 Answers2

2

When you deserialize and get nulls - chances are good that it's one of 2 problems:

  1. The concrete class you are deserializing into doesn't match the fields returned in the json you are deserializing, or

  2. You are deserializing to a field that doesn't have a setter

There are other possible problems, but those 2 cover about 90% of the errors I see in code review.

In your case, you've fallen victim to the first one -- your class and your json dont matchup. Based on the sample you provided, I think your code is expecting something like the structure below.

Public class TraderFields {
   public string Users {get; set;}  
   public string macaddress_value {get; set; }
   public string email_value {get;set;}
}
Public class TraderRecord {
   public string id {get;set;}
   public TraderFields fields {get;set;} = New TraderFields();
   public DateTime createdTime {get;set;}
}
Public class TraderData {
   public List<TraderRecord> records {get; set; } = New List<TraderRecord>()
}

You'll see the same thing in other answers, Im sure. FYI - That's also why most of us use tools to auto-generate classes from json data (we all miss stuff when coding classes by hand). Good luck with the fix!

bri
  • 2,932
  • 16
  • 17
  • Thank you so much, i really appreciate that, maybe it's something foolish but i was trying to solve this for hours. Im newbie to C# and fall in this bit problem. Thanks :)))) – SkyMixed Apr 23 '18 at 20:44
0

Your model needs to be adjusted to something like this:

public class TraderDataRecords 
{ 
    public TraderDataRecords() { 
        Records = new TraderData[0];
    }
    public TraderData[] Records { get; set; } 
}

Note: also your data sample has an extra brace after the email...

See below

{"records":[{"id":"reczOujwG3H1DuhR7","fields":{"Users":"user_juseh","macaddress_value":"123456789","email_value":"juseh@hotmail.com"**}**,"createdTime":"2018-04-21T22:36:48.000Z"}]}
Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73