0

我正在尝试反序列化一些 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;
        }
    
4

2 回答 2

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)

于 2014-08-05T06:38:46.013 回答
1

响应字符串不能逐字与您的数据模型一起使用。从“价值”开始。

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

hth

于 2014-08-05T03:30:59.783 回答