在asp.net中反序列化Json

发布于 2024-12-05 08:15:43 字数 1459 浏览 0 评论 0原文

我有一个 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; }
    }

}

如果有人做过这样的工作请回复。

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,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);

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双马尾 2024-12-12 08:15:43

很难理解你的问题,但如果我没记错 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 而不是此处的原始 Web 请求。您可能还想查看 Json.net,因为它是一个比 JavaScriptSerializer 更好的序列化库。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文