在asp.net中反序列化Json
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难理解你的问题,但如果我没记错 BrightCove 的 API,我想你想在这里制作一个 BrightCove 视频的集合。因此,您需要的是一个 2 层对象 - 一个包含视频数组的播放列表的容器。你所得到的只是覆盖容器。它应该看起来像:
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:
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.