How to deserialize json in asp.net

I have a code that asks the website

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

After running this code, I will get this json type

 [ { "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" } ] 

Now I want to deserialize this in asp.net (C #)
I tried to get the correct answer, but did not get it.

Please advice.

+7
source share
3 answers

Create a class called FromFlix inseide App_Code similar to this

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

Now, after the while loop ends, do it.

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

response is a List<FromFlix> , i.e. a general list of type FromFlix
Here's 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&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.

+10
source

You can use the JavaScriptSerializer type to serialize and deserialize JSON data.

 var serializer = new JavaScriptSerializer(); var deserialized = serializer.Deserialize<TheTypeToWhichJSONWillMap>(myJson); 

EDIT:

I'm not sure what problems you have with this, but the following is a working example with the JSON string you provide:

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

This leaves us List<TheType> with a 14 Element List<TheType> .

+4
source

What you need to do is check out JSON.NET http://json.codeplex.com/ to make your life a lot easier

+2
source

All Articles