1

この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 /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);
}
4

1 に答える 1

0

現在のjson構造によると、これは機能するはずです

var items = serializer.Deserialize<List<Url>>(json); // Url instead of Response

これがうまくいかない場合は、Responseクラスの構造を反映するためにjson構造を変更する必要があります。

私が何かを逃さない限り、これは必要なjson構造です

[{ fileUrl:{ fileUrl: "localhost/ContentManager/get/ovYWB0/81/wallpaper" }}]
于 2012-08-30T13:03:57.007 に答える