在asp.net中反序列化Json

时间:2011-09-20 17:43:44

标签: asp.net json deserialization

我有一个Json,我想反序列化并将其保存在类的属性中。但它没有使用该类序列化,是否可以将Json值存储在此类的属性中。

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://api.brightcove.com/services/library?command=find_playlist_by_id&token=myToken&playlist_id=61674080001&video_fields=id,name,thumbnailURL,playsTotal,shortDescription&page_size=1&sort_by=plays_total&sort_order=desc");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        using (StreamReader reads = new StreamReader(response.GetResponseStream()))
        {
            value = reads.ReadToEnd();
        }
        value = value.Remove(1, value.IndexOf("]") + 1);
        hidField.Value = value;



        JavaScriptSerializer s = new JavaScriptSerializer();
        BCVideos v =  s.Deserialize<BCVideos>(value);

存储Json数据的类。

public class BCVideos
{
    private int _id;

    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }
    private string _name, _thumbnailUrl, _shortDescription;

    public string ShortDescription
    {
        get { return _shortDescription; }
        set { _shortDescription = value; }
    }

    public string ThumbnailUrl
    {
        get { return _thumbnailUrl; }
        set { _thumbnailUrl = value; }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

}

如果有人做过这种工作,请回复。

1 个答案:

答案 0 :(得分:0)

很难回答你的问题,但如果我能正确记住BrightCove的API,我想你想在这里制作BrightCove视频的集合。所以你需要的是一个2层对象 - 一个容器,用于播放列表,其中包含一系列视频。你所拥有的只是盖住容器。它应该看起来像:

public class BrightCovePlayList
{
    private int _id;

    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }
    private string _name, _thumbnailUrl, _shortDescription;

    public string ShortDescription
    {
        get { return _shortDescription; }
        set { _shortDescription = value; }
    }

    public string ThumbnailUrl
    {
        get { return _thumbnailUrl; }
        set { _thumbnailUrl = value; }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public Video[] Videos {get; set; }
}

public class Video
{
    public int Id {get; set;}
    public string Name {get; set;}
    //properties as appropriate.
}

PS:您可能希望在此处使用Hammock或至少System.Net.WebClient之类的内容,而不是原始网络请求。您还可以查看Json.net,因为它是比JavaScriptSerializer更好的序列化库。