0

I'm trying to deserialize some OneNote API results. Below is my:

  1. Example result from notebook query
  2. Sample Class
  3. Code to deserialize (two attempts obj1 and obj2

    Content-Type: application/json
    X-CorrelationId: <GUID>
    Status: 200 OK
    {
      "@odata.context":"https://www.onenote.com/api/v1.0/$metadata#notebooks","value":[
        {
          "isDefault":false,
    "userRole":"Contributor",
    "isShared":true,
    "sectionsUrl":"https://www.onenote.com/api/v1.0/notebooks/notebook ID/sections",
    "sectionGroupsUrl":"https://www.onenote.com/api/v1.0/notebooks/notebook ID/sectionGroups",
    "links":{
            "oneNoteClientUrl":{
              "href":"https:{client URL}"
            },"oneNoteWebUrl":{
              "href":"https://{web URL}"
            }
          },
    "id":"notebook ID",
    "name":"notebook name",
    "self":"https://www.onenote.com/api/v1.0/notebooks/notebook ID",
    "createdBy":"user name",
    "lastModifiedBy":"user name",
    "createdTime":"2013-10-05T10:57:00.683Z",
    "lastModifiedTime":"2014-01-28T18:49:00.47Z"
        },{
          "isDefault":true,
    "userRole":"Owner",
    "isShared":false,
    "sectionsUrl":"https://www.onenote.com/api/v1.0/notebooks/notebook ID/sections",
    "sectionGroupsUrl":"https://www.onenote.com/api/v1.0/notebooks/notebook ID/sectionGroups",
    "links":{
            "oneNoteClientUrl":{
              "href":"https://{client URL}"
            },"oneNoteWebUrl":{
              "href":"https://{web URL}"
            }
          },
    "id":"notebook ID",
    "name":"notebook name",
    "self":"https://www.onenote.com/api/v1.0/notebooks/notebook ID",
    "createdBy":"user name",
    "lastModifiedBy":"user name",
    "createdTime":"2011-07-20T03:54:46.283Z",
    "lastModifiedTime":"2014-06-24T20:49:42.227Z"
        }
      ]
    }
    
    
     [DataContract]
        public class Notebooks
        {
            [DataMember]
            public bool isDefault { get; set; }
            [DataMember]
            public string userRole { get; set; }
            [DataMember]
            public string isShared { get; set; }
            [DataMember]
            public string sectionsUrl { get; set; }
            [DataMember]
            public string sectionGroupsUrl { get; set; }
            [DataMember]
            public string oneNoteWebUrl { get; set; }
            [DataMember]
            public string name { get; set; }
            [DataMember]
            public string self { get; set; }
            [DataMember]
            public string createdBy { get; set; }
            [DataMember]
            public string lastModifiedBy { get; set; }
            [DataMember]
            public string lastModifiedTime { get; set; }
            [DataMember]
            public string id { get; set; }
            [DataMember]
            public string createdTime { get; set; }
        }
    

    // This sample web string returned from the Web Request is stored in this textbox string resultStr = resultTextBox.Text.ToString();

             var obj1 = DeserializeJSon<List<Notebooks>>(resultStr);
    
            foreach (Notebooks nb in obj1)
            {
               string id = nb.ToString();
            }
    
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Notebooks>));
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(resultStr));
    
           var obj2 = (List<Notebooks>)ser.ReadObject(stream);
    
            foreach (Notebooks nb in obj2)
            {
                string id = nb.id.ToString();
            }
    
    
        public static T DeserializeJSon<T>(string jsonString)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            T obj = (T)ser.ReadObject(stream);
            return obj;
        }
    
Nizam
  • 4,569
  • 3
  • 43
  • 60
Trey Balut
  • 1,355
  • 3
  • 19
  • 39

2 Answers2

2

You are not being able to deserialize the result because the JSON returned by the OneNote API is not a List of Notebook objects. It is one main object with two properties: "@odata.context" and "value". "value" itself is a list of Notebook objects.

I suggest you make a class like the following

public class OneNoteJsonResponse
{
    [DataMember(Name = "@odata.context")]
    public string ODataContext {get; set;}
    [DataMember]
    public List<Notebook> value {get; set;}
}

Then try deserializing the response using DataContractSerializer following this example: Deserialize JSON with C#

I personally would recommend using JSON.NET instead of the DataContractSerializer, as it provides more flexibility and better performance. You can install it easily using nuget. http://james.newtonking.com/json

Let us know if you have any issues, happy coding!

EDIT: Also, the Notebook object you have is missing the "links" object, which would be another class of its own. (containing the oneNoteClientUrl and OneNoteWebUrl)

Community
  • 1
  • 1
Jorge Aguirre
  • 2,787
  • 3
  • 20
  • 27
1

response string cannot be used verbatim with your data model. start from "value".

alternatively, have a look at http://json2csharp.com/

hth