Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to deserialize json in asp.net

Tags:

json

c#

asp.net

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&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 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" }
]

Now i want deserialize this in asp.net(c#)
I tried to get a proper answer but didn't get.

Please advice.

like image 400
Atul Avatar asked Aug 26 '11 11:08

Atul


People also ask

How does JSON deserialize work C#?

In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it calls the static method DeserializeObject() of the JsonConvert class by passing JSON data. It returns a custom object (BlogSites) from JSON data.

What is the use of JSON deserialize?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.

What is serialize JSON?

Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). Serialization is the process of converting the state of an object, that is, the values of its properties, into a form that can be stored or transmitted.


2 Answers

Create a class called FromFlix inseide App_Code like this

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

Now after the end of your while loop, do this.

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

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

like image 150
naveen Avatar answered Sep 20 '22 09:09

naveen


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 of what problems you're having 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 with deserialized being an List<TheType> with 14 elements.

like image 38
Grant Thomas Avatar answered Sep 22 '22 09:09

Grant Thomas