Deserialize OneNote Notebooks API Response

82 views Asked by At

I'm getting an empty object when I try to Deserialize a OneNote GetAllNotebooks query.

string[] tempCapture = null;
var url = new Uri("https://graph.microsoft.com/v1.0/me/onenote/notebooks");
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (IsAuthenticated)
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
}
var response = await client.GetAsync(url);
var result = await response.Content.ReadAsStringAsync();
tbResponse.Text = result.ToString();
DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(List<Value>));
MemoryStream stream1 = new MemoryStream(Encoding.UTF8.GetBytes(tbResponse.Text.ToString()));
var obj1 = (List<Value>)ser1.ReadObject(stream1);

I'm trying to get a list of notebooks, names, links to add to a database. And my table structure matches the class below.

Here is my OneNote API class

public class Value
{
    public bool isDefault { get; set; }
    public string userRole { get; set; }
    public bool isShared { get; set; }
    public string sectionsUrl { get; set; }
    public string sectionGroupsUrl { get; set; }
    public Links links { get; set; }
    public string name { get; set; }
    public string self { get; set; }
    public string createdBy { get; set; }
    public string lastModifiedBy { get; set; }
    public string lastModifiedTime { get; set; }
    public string id { get; set; }
    public string createdTime { get; set; }
}

Here is my new code with the RootObject. I'm still getting an error. It is in the catch exception.

 var test = await client.GetAsync(url);
string testStr = await test.Content.ReadAsStringAsync();
DataContractJsonSerializer serial = new DataContractJsonSerializer(typeof(RootObject));
MemoryStream testStream = new MemoryStream(Encoding.UTF8.GetBytes(testStr));
try
{
    var objx = (List<RootObject>)serial.ReadObject(testStream);
}
catch(Exception ex)
{
    ex.ToString();
    //"There was an error deserializing the object of type OneNoteSOAP2.RootObject. End element 'createdBy' from namespace '' expected. Found element 'user' from namespace ''."
}
1

There are 1 answers

4
Jorge Aguirre On

You can use http://json2csharp.com/. Basically, just copy the value of our JSON being returned, and use the classes generated by this website. Use RootObject to deserialize.

I ran this for you and obtained these classes:

public class OneNoteClientUrl
{
    public string href { get; set; }
}

public class OneNoteWebUrl
{
    public string href { get; set; }
}

public class Links
{
    public OneNoteClientUrl oneNoteClientUrl { get; set; }
    public OneNoteWebUrl oneNoteWebUrl { get; set; }
}

public class Value
{
    public string id { get; set; }
    public string self { get; set; }
    public string createdTime { get; set; }
    public string name { get; set; }
    public string createdBy { get; set; }
    public string lastModifiedBy { get; set; }
    public string lastModifiedTime { get; set; }
    public bool isDefault { get; set; }
    public string userRole { get; set; }
    public bool isShared { get; set; }
    public string sectionsUrl { get; set; }
    public string sectionGroupsUrl { get; set; }
    public Links links { get; set; }
}

public class RootObject
{
    public List<Value> value { get; set; }
}