What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 11281 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > ASP.NET AJAX > How to serialize object into JSON Format? ...
Raja

How to serialize object into JSON Format?

 Code Snippet posted by: Raja | Posted on: 8/14/2009 | Category: ASP.NET AJAX Codes | Views: 3428 | Status: [Member] | Alert Moderator   


To searialize object into JSON format, use following method.

Namespace to use System.Runtime.Serialization.Json;

private static string SerializeObjectIntoJson(Person p)

{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(p.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, p);
ms.Flush();

byte[] bytes = ms.GetBuffer();
string jsonString = Encoding.UTF8.GetString(bytes, 0, bytes.Length).Trim('\0');
return jsonString;
}
}


Note above method and Namespace is accessible only in Website or Web application project not in class library.

Reference: http://www.dotnetfunda.com/articles/article454-using-pagemethods-and-json-in-aspnet-ajax.aspx

Regards,
Raja, USA
Found interesting? Add this to:


 Responses

Poster
Posted by: Poster | Posted on: 8/26/2009 | Level: Starter | Status: [Member] | Alert Moderator 

Thanks, this is what I was looking for.

Regards

>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/23/2013 9:36:55 PM