反序列化OneNote JSon结果

时间:2014-08-05 00:57:31

标签: json onenote

我试图反序列化一些OneNote API结果。以下是我的:

  1. 笔记本查询的示例结果
  2. 样本类
  3. 反序列化的代码(两次尝试obj1和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; }
        }
    

    //从Web请求返回的此示例Web字符串存储在此文本框中 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;
        }
    

2 个答案:

答案 0 :(得分:2)

您无法反序列化结果,因为OneNote API返回的JSON不是Notebook对象列表。它是一个具有两个属性的主要对象:“@ odata.context”和“value”。 “value”本身就是Notebook对象的列表。

我建议你创建一个类似下面的课程

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

然后尝试使用DataContractSerializer反序列化响应,如下例所示: Deserialize JSON with C#

我个人建议使用JSON.NET而不是DataContractSerializer,因为它提供了更多的灵活性和更好的性能。您可以使用nuget轻松安装它。 http://james.newtonking.com/json

如果您有任何问题,请告诉我们,编码愉快!

编辑:此外,您拥有的Notebook对象缺少“链接”对象,这将是它自己的另一个类。 (包含oneNoteClientUrl和OneNoteWebUrl)

答案 1 :(得分:1)

响应字符串不能与您的数据模型逐字使用。从&#34;值&#34;开始。

或者,请查看http://json2csharp.com/

HTH