1
我有這樣的代碼進行反序列化JSON這個,但我不斷收到此錯誤:如何反序列化JSON這個
"Cannot convert object of type System.String to type Namespace.Models.Url"
。 JSON的結構如下:
[ { fileUrl: "localhost/ContentManager/get/ovYWB0/81/wallpaper" }, { fileUrl: "localhost/ContentManager/get/AcjwO0/81/wallpaper" }, { fileUrl: "localhost/ContentManager/get/HCR0q0/81/wallpaper" } ]
類我正在映射到爲如下:
public class Url { public string FileUrl { get; set; } } public class Response { public Url FileUrl { get; set; } }
的反序列化的代碼如下:
var serializer = new JavaScriptSerializer();
IList<MTContribute> data = new List<MTContribute>();
var items = serializer.Deserialize<List<Response>>(json);
foreach (var item in items)
{
var newData = new MTContribute
{
DateCreated = DateTime.Today,
IsActive = Convert.ToBoolean("True"),
MTContributeCategoryId = Category.MTContributeCategoryId,
Url = item.FileUrl.FileUrl
};
data.Add(newData);
}