开发者

Deserializing Json in asp.net

i have a Json, i want to deserialize and save it inside the property of the class. but it has not been serialized using that class, is it possible to store the Json value in this class's property..

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,shortDescr开发者_JS百科iption&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);

class for the Storing Json Data.

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

}

Please reply if anyone have done this kind of work.


It is pretty difficult to follow your question, but I think you want to make a collection of BrightCove videos here if I remember BrightCove's API correctly. So what you'd need is a 2-layered object -- one container for the playlists that has an array of Videos. What you've got just covers the container. It should look something like:

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: You probably want to use something like Hammock or at least System.Net.WebClient rather than raw webrequests here. You also might want to look at Json.net as it is a much better serialization library than the JavaScriptSerializer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜