2017-09-06 2 views
0

OneNote GetAllNotebooks 쿼리를 deserialize하려고하면 빈 개체가 나타납니다.Deserialize 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의 값을 복사하고이 웹 사이트에서 생성 한 클래스를 사용하십시오. deserialize하려면 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 = 새 DataContractJsonSerializer (typeof (RootObject)); –

관련 문제