如何反序列化在asp.net JSON [英] how to deserialize json in asp.net
本文介绍了如何反序列化在asp.net JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有跟随的代码从Web请求
I have follow code which request from web
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.bigflix.com/BIGFlixApi.do?parameter=getProductType&partnerID=17&uniqueID=54325345435×tamp=131286916367&digest=bf53cae8f364cfc1d796489d09e4cfd  <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.net这个反序列化(C#)结果
我试图让一个合适的答案,但没有得到。
Now i want deserialize this in asp.net(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("http://api.bigflix.com/BIGFlixApi.do?parameter=getProductType&partnerID=17&uniqueID=54325345435×tamp=131286916367&digest=bf53cae8f364cfc1d796489d09e4cfd  <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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文