我有这段代码来反序列化这个 json,但我不断收到这个错误:
“无法将 System.String 类型的对象转换为 Namespace.Models.Url 类型”
. json结构如下:
[ { fileUrl: "localhost/ContentManager/get/ovYWB0/81/wallpaper" }, { fileUrl: "localhost/ContentManager/get/AcjwO0/81/wallpaper" }, { fileUrl: "localhost/ContentManager/get/HCR0q0/81 /墙纸” } ]
我映射到的类如下:
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);
}