2011-08-26 77 views
7

我跟隨代碼從網絡如何反序列化JSON在asp.net

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#)
我試圖得到一個正確的答案,但沒有得到。

請指教。

回答

10

while循環結束後這樣

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

創建一個名爲FromFlix inseide App_Code文件類現在,做到這一點。

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

希望這會有所幫助。

+0

先生這是給錯誤錯誤不能隱式轉換類型'System.Collections.Generic .IList '到'string' – Atul

+0

你在這行後做了什麼?在此之後我寫回應 – naveen

+0

。寫(性反應); – Atul

2

你需要的是JSON.NET檢查此鏈接了http://json.codeplex.com/會讓你的生活容易得多

+2

erm爲什麼這是-1? – Bobby

+0

+1,因爲它被低估。 @downvoter:點擊開心? – naveen

+0

Thanks Naveen = o) – Bobby

4

可以使用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是一個List<TheType>與14個元素。

+0

先生我試試這個,但不工作... – Atul

+0

@Atul,**如何**它不工作? –

+0

@ Moo果汁,先生,這不是工作 – Atul