c# - asp.net如何反序列化json

标签 c# asp.net json

我有遵循从网络请求的代码

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&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

[
    { "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#) 中反序列化它
我试图得到正确的答案,但没有得到。

请指教。

最佳答案

像这样在 App_Code 中创建一个名为 FromFlix 的类

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

responseList<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&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/>");
}

希望这对您有所帮助。

关于c# - asp.net如何反序列化json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7203770/

相关文章:

c# - 将 overdueDate 从 DateTime 转换为格式为 dd/mm/yyyy 00 :00:00 的字符串

javascript - 使用 2 个 key 从 JSON AngularJS 接收数据

jQuery + JSONP 返回数据为空?

java - 如何给json对象命名

c# - Windows Store In App Purchase问题 “Value does not fall within the expected range”

c# - 从线程增加 Int 时的竞争条件

c# - System.AccessViolationException : Attempted to read or write protected memory. 这通常表示其他内存已损坏

c# - 在 MVC 中填充下拉列表

c# - 如何在 Visual C# .NET Web 元素中添加对所有 Web 窗体的 CSS 样式表引用

asp.net - 在 asp.net 中创建条形码