反序列化OneNote笔记本API响应

时间:2017-09-06 21:48:55

标签: json onenote onenote-api

当我尝试反序列化OneNote GetAllNotebooks查询时,我得到一个空对象。

        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);

我试图获取笔记本,名称和添加到数据库的链接列表。我的表结构与下面的类匹配。

这是我的OneNote API类

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; }
}

这是我使用RootObject的新代码。我仍然得到一个错误。这是捕获例外。

            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 个答案:

答案 0 :(得分:0)

您可以使用http://json2csharp.com/。基本上,只需复制我们返回的JSON的值,并使用此网站生成的类。使用RootObject反序列化。

我为你跑了这个并获得了这些课程:

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; }
}