Serialize Object in .Net to JSON

Jak
Jak
Member
352 Points
151 Posts

Hi,

Serialize Object in .Net to JSON:

I want in this format:

 

{"detail":[{"IsChild":"N","MenuId":1,"MenuName":"Black"},{"IsChild":"N","MenuId":2,"MenuName":"White"}],"success":"1"}
Views: 8125
Total Answered: 2
Total Marked As Answer: 1
Posted On: 10-Mar-2015 18:36

Share:   fb twitter linkedin
Answers
NiceOne Team
NiceOne...
Admin
408 Points
69 Posts
Marked as answer      

Hi, Rahul

You can use following library:

  • JavaScriptSerializer
  • NewtonSoft.Json

JavaScriptSerializer:

 

public class Menu
{
public List<MenuApp> detail{get;set;}
public string success{ get; set; }
}
 
[Serializable]
public class MenuApp
{
[DataMember]
public int MenuId;
[DataMember]
public string MenuName;
[DataMember]
public string IsChild;
}
 
 
public static void GetMenu()
{
Menu Menu = new Menu();
List<MenuApp> MenuAppList = new List<MenuApp>();
MenuApp MenuApp;
string JSON_String;
MenuApp = new MenuApp();
MenuApp.MenuId = 1;
MenuApp.MenuName = "Black";
MenuApp.IsChild = "N";
MenuAppList.Add(MenuApp);
MenuApp = new MenuApp();
MenuApp.MenuId = 2;
MenuApp.MenuName = "White";
MenuApp.IsChild = "N";
MenuAppList.Add(MenuApp);
Menu.detail = MenuAppList;
Menu.success = "1";
JSON_String = Newtonsoft.Json.JsonConvert.SerializeObject(Menu);
}

 

 

Posted On: 11-Mar-2015 21:26
Rashmi
Rashmi
Member
78 Points
24 Posts
     

Hi,

You could use the JavaScriptSerializer class:

using System.Web.Script.Serialization;
var json = new JavaScriptSerializer().Serialize(obj);

Example:

var JSONString=  new JavaScriptSerializer().Serialize(object);
Posted On: 22-Jun-2015 01:14
banner

Blog

Active User (2)

 Log In to Chat