如何反序列化在asp.net JSON

编程入门 行业动态 更新时间:2024-10-27 22:25:26
本文介绍了如何反序列化在asp JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有跟随的代码从Web请求

I have follow code which request from web

StringBuilder sb = new StringBuilder(); byte[] buf = new byte[8192]; HttpWebRequest request = (HttpWebRequest)WebRequest.Create("api.bigflix/BIGFlixApi.do?parameter=getProductType&partnerID=17&uniqueID=54325345435&timestamp=131286916367&digest=bf53cae8f364cfc1d796489d09e4cfd&nbsp&nbsp<br>"); HttpWebResponse responce = (HttpWebResponse)request.GetResponse(); Stream resstream = responce.GetResponseStream(); string tempString = null; int count = 0; do { count = resstream.Read(buf, 0, buf.Length); if (count != 0) { tempString = Encoding.ASCII.GetString(buf, 0, count); sb.Append(tempString); } } while (count > 0); { Response.Write(sb.ToString() + "<br/><br/>"); // string[] val = sb.ToString().Split('"'); }

运行这段代码我会得到这种类型的JSON

After the run this code i will get this type of json

[ { "id": 23, "name": "Video Clips" }, { "id": 15, "name": "Deleted Scenes" }, { "id": 9, "name": "Music Albums" }, { "id": 7, "name": "Trailers" }, { "id": 18, "name": "Short Films" }, { "id": 21, "name": "Movie Clips" }, { "id": 1, "name": "Movies " }, { "id": 4, "name": "Plays" }, { "id": 22, "name": "Scenes" }, { "id": 2, "name": "TV Show" }, { "id": 5, "name": "Kids" }, { "id": 16, "name": "Interviews" }, { "id": 11, "name": "Film Songs" }, { "id": 14, "name": "Making of Movie" } ]

现在我想在asp这个反序列化(C#)结果我试图让一个合适的答案,但没有得到。

Now i want deserialize this in asp(c#) I tried to get a proper answer but didn't get.

请咨询。

推荐答案

创建一个名为FromFlix inseide App_Code文件像类这种

Create a class called FromFlix inseide App_Code like this

public class FromFlix { public string ID { get; set; } public string Name { get; set; } }

现在while循环结束后,做到这一点。

Now after the end of your while loop, do this.

JavaScriptSerializer ser = new JavaScriptSerializer(); var response = ser.Deserialize<IList<FromFlix>>(sb.ToString());

响应是列表< FromFlix方式> ,也就是类型FromFlix结果$ b $的一个通用的b名单这是你应该如何使用它

The response is a List<FromFlix>, that is, a generic list of type FromFlix This is how you should use it.

StringBuilder sb = new StringBuilder(); byte[] buf = new byte[8192]; HttpWebRequest request = (HttpWebRequest)WebRequest.Create("api.bigflix/BIGFlixApi.do?parameter=getProductType&partnerID=17&uniqueID=54325345435&timestamp=131286916367&digest=bf53cae8f364cfc1d796489d09e4cfd&nbsp&nbsp<br>"); HttpWebResponse responce = (HttpWebResponse)request.GetResponse(); Stream resstream = responce.GetResponseStream(); string tempString = null; int count = 0; do { count = resstream.Read(buf, 0, buf.Length); if (count != 0) { tempString = Encoding.ASCII.GetString(buf, 0, count); sb.Append(tempString); } } while (count > 0); JavaScriptSerializer ser = new JavaScriptSerializer(); List<FromFlix> response = ser.Deserialize<List<FromFlix>>(sb.ToString()); foreach (var item in response) { Response.Write("ID: " + item.ID + "&" + "Name: " + item.Name + "<br/>"); }

希望这有助于。

Hope this helps.

更多推荐

如何反序列化在asp.net JSON

本文发布于:2023-11-13 23:27:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585610.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   asp   JSON   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!