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);
}