0

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 ''."
}
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Trey Balut
  • 1,355
  • 3
  • 19
  • 39

1 Answers1

0

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; }
}
Jorge Aguirre
  • 2,787
  • 3
  • 20
  • 27
  • Thanks Jorge, Is my code correct then. Or do I need to change the Deserialize line? – Trey Balut Sep 06 '17 at 23:58
  • DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(RootObject)); – Jorge Aguirre Sep 07 '17 at 00:06
  • "There was an error deserializing the object of type OneNoteSOAP2.RootObject. End element 'createdBy' from namespace '' expected. Found element 'user' from namespace ''." It occurs right after the " var objx = (List)serial.ReadObject(testStream); " statement. btw Thanks for your help. Im trying to reconstruct the github Windows Universal app. But it is overkill and hard to follow. – Trey Balut Sep 07 '17 at 03:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153830/discussion-between-trey-balut-and-jorge-aguirre). – Trey Balut Sep 07 '17 at 04:25