我在Sharepoint 2013中搜索了一個JSON文本,並想將其反序列化爲MyClass列表。 我有使用KeyValuePair將Sharepoint 2013搜索結果中的JSON反序列化爲MyClass列表
var results = item["Cells"]["results"].ToObject<List<KeyValuePair<string, string>>>();
溶液但這種方法除去的日期格式等一切最終成爲字符串。例如。在JSON文件中的正確的挪威日期最終成爲準文字中的Norwenglish日期。
編輯:我正在使用Newtonsoft.Json庫。
編輯:日期格式只是問題的一部分。反序列化是我的主要問題。
這個例子簡化了JSON和class。 JSON文件可以有許多這樣的記錄,所以它最終會出現在對象列表中。你有一個很好的解決方案來反序列化這個JSON嗎?
JSON
{
"__metadata": {
"type": "SP.SimpleDataRow"
},
"Cells": {
"results": [
{
"__metadata": {
"type": "SP.KeyValue"
},
"Key": "Id",
"Value": "358553",
"ValueType": "Edm.Int64"
},
{
"__metadata": {
"type": "SP.KeyValue"
},
"Key": "Url",
"Value": "http://somewhere.com",
"ValueType": "Edm.String"
},
{
"__metadata": {
"type": "SP.KeyValue"
},
"Key": "Title",
"Value": "My title",
"ValueType": "Edm.String"
},
{
"__metadata": {
"type": "SP.KeyValue"
},
"Key": "MyDate",
"Value": "2017-09-10T11:10:19Z",
"ValueType": "Edm.DateTime"
}
]
}
}
類
public class MyClass
{
public int Id { get; set; }
public string Url { get; set; }
public string Title { get; set; }
public DateTime MyDate { get; set; }
}
如何使用合適的Json庫? NET中常用的是Json.Net – Steve
對不起,忘了通知使用中的Json庫。我正在使用Newtonsoft.Json庫。 – Srednasoft
無論你的日期格式是什麼,你都需要指定一個'IsoDateTimeConverter'。 Json.Net預計[ISO 8601日期](https://www.iso。org/iso-8601-date-and-time-format.html) – Liam