2017-09-06 6 views
0

OneNote GetAllNotebooksクエリをデシリアライズしようとすると空のオブジェクトが表示されます。OneNoteノートブックAPIレスポンスを逆シリアル化する

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

データベースに追加するノートブック、名前、リンクのリストを取得しようとしています。私のテーブル構造は以下のクラスと一致します。

はここ。ここRootObjectと私の新しいコードである私の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; } 
} 

です。私はまだエラーが発生しています。これはcatch例外にあります。

  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 ''." 
     } 

答えて

0

http://json2csharp.com/を使用できます。基本的に、返されるJSONの値をコピーし、このWebサイトで生成されたクラスを使用します。デシリアライズするには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; } 
} 
+0

おかげでホルヘに、正しい私のコードです。またはDeserialize行を変更する必要がありますか? –

+0

コードが間違っています。 –

+0

を使用してください。DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(RootObject)); –

関連する問題