0

So here's what I'm getting back from the oData service...

{  
   "odata.metadata":"http://server.ca/Mediasite/Api/v1/$metadata#UserProfiles",
   "value":[  
      {  
         "odata.id":"http://server.ca/Mediasite/Api/v1/UserProfiles('111111111111111')",
         "QuotaPolicy@odata.navigationLinkUrl":"http://server.ca/Mediasite/Api/v1/UserProfiles('111111111111111')/QuotaPolicy",
         "#SetQuotaPolicyFromLevel":{  
            "target":"http://server.ca/Mediasite/Api/v1/UserProfiles('111111111111111')/SetQuotaPolicyFromLevel"
         },
         "Id":"111111111111111",
         "UserName":"testuser",
         "DisplayName":"testuser Large",
         "Email":"testuser@testuser.ca",
         "Activated":true,
         "HomeFolderId":"312dcf4890df4b129e248a0c9a57869714",
         "ModeratorEmail":"testuser@testuserlarge.ca",
         "ModeratorEmailOptOut":false,
         "DisablePresentationContentCompleteEmails":false,
         "DisablePresentationContentFailedEmails":false,
         "DisablePresentationChangeOwnerEmails":false,
         "TimeZone":26,
         "PresenterFirstName":null,
         "PresenterMiddleName":null,
         "PresenterLastName":null,
         "PresenterEmail":null,
         "PresenterPrefix":null,
         "PresenterSuffix":null,
         "PresenterAdditionalInfo":null,
         "PresenterBio":null,
         "TrustDirectoryEntry":null
      }
   ]
}

I want to deserialize this into a simple class, like just the important stuff (Id, Username, etc...to the end).

I have my class create, but for the life of me I can't figureout how to throw away all the wrapper objects oData puts around this thing.

Can anyone shed some light?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Steve McNiven-Scott
  • 1,770
  • 2
  • 15
  • 29

1 Answers1

2

You can use JsonObject do dynamically traverse the JSON, e.g:

var users = JsonObject.Parse(json).ArrayObjects("value")
    .Map(x => new User
    {
        Id = x.Get<long>("Id"),
        UserName = x["UserName"],
        DisplayName = x["DisplayName"],
        Email = x["Email"],
        Activated = x.Get<bool>("Activated"),
    });

users.PrintDump();

Or deserialize it into a model that matches the shape of the JSON, e.g:

public class ODataUser
{
    public List<User> Value { get; set; }
}

public class User
{
    public long Id { get; set; }
    public string UserName { get; set; }
    public string DisplayName { get; set; }
    public string Email { get; set; }
    public bool Activated { get; set; }
    public string HomeFolderId { get; set; }
    public string ModeratorEmail { get; set; }
    public bool ModeratorEmailOptOut { get; set; }
    public bool DisablePresentationContentCompleteEmails { get; set; }
    public bool DisablePresentationContentFailedEmails { get; set; }
    public bool DisablePresentationChangeOwnerEmails { get; set; }
    public int TimeZone { get; set; }
}

var odata = json.FromJson<ODataUser>();
var user = odata.Value[0];
mythz
  • 141,670
  • 29
  • 246
  • 390