I have this code to deserialize this json but i keep getting this error:
“Cannot convert object of type System.String to type
Namespace.Models.Url”
. The json structure is as below:
[ { fileUrl: “localhost/ContentManager/get/ovYWB0/81/wallpaper” }, {
fileUrl: “localhost/ContentManager/get/AcjwO0/81/wallpaper” }, {
fileUrl: “localhost/ContentManager/get/HCR0q0/81/wallpaper” }
]
The class i am mapping to is as below:
public class Url { public string FileUrl { get; set; } } public class Response { public Url FileUrl { get; set; } }
The deserializing code is as below:
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);
}
According your current json structure this should work
if this won’t work for you, you have to change the json structure in order to reflect the structure of
Responseclass.Unless I missed something, this is the necessary json structure