如何在asp.net中反序列化json
本文关键字:反序列化 json net asp | 更新日期: 2023-09-27 18:06:39
我有来自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
[
{ "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#(中反序列化它
我试图得到一个恰当的答案,但没有得到。
请提供建议。
创建一个名为FromFlix的类inseide App_Code,如下
public class FromFlix
{
public string ID { get; set; }
public string Name { get; set; }
}
现在,在while循环结束后,执行此操作。
JavaScriptSerializer ser = new JavaScriptSerializer();
var response = ser.Deserialize<IList<FromFlix>>(sb.ToString());
response
是List<FromFlix>
,即FromFlix类型的泛型列表
这就是你应该如何使用它。
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/>");
}
希望这能有所帮助。
您可以使用JavaScriptSerializer
类型来序列化和反序列化JSON数据。
var serializer = new JavaScriptSerializer();
var deserialized = serializer.Deserialize<TheTypeToWhichJSONWillMap>(myJson);
编辑:
我不确定您在这方面遇到了什么问题,但下面是您提供的JSON字符串的一个工作示例:
static string TheJson = "...";
public class TheType
{
public int id { get; set;}
public string name { get; set; }
}
var serializer = new JavaScriptSerializer();
var deserialized = serializer.Deserialize<List<TheType>>(TheJson);
这使得deserialized
是具有14个元素的List<TheType>
。