Convert Class Properties to Json string using C# and VB.Net in ASP.Net

makumbi
 
on Aug 30, 2020 11:27 PM
Sample_896348.zip
3421 Views

How to convert this code in to json

Dim Subscriptiondetails As Map
Subscriptiondetails.Initialize
Subscriptiondetails.put("username","5")
Subscriptiondetails.put("password", "ty3")
Subscriptiondetails.put("api","depositmobilemoney")
Subscriptiondetails.put("msisdn",Phones.Text)
Subscriptiondetails.put("amount",10000)
Subscriptiondetails.put("narration","Subscription")
Subscriptiondetails.put("reference","sbscr")
Subscriptiondetails.put("status_notification_url","http://108.175.14.205/SMISApi/api/smis/subscriptionresponse")

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 31, 2020 06:22 AM

Hi makumbi,

Refer below code.

C#

protected void Page_Load(object sender, EventArgs e)
{
    Map Subscriptiondetails = new Map();
    Subscriptiondetails.username = "5";
    Subscriptiondetails.password = "ty3";
    Subscriptiondetails.api = "depositmobilemoney";
    Subscriptiondetails.msisdn = "Phones.Text";
    Subscriptiondetails.amount = 10000;
    Subscriptiondetails.narration = "Subscription";
    Subscriptiondetails.reference = "sbscr";
    Subscriptiondetails.status_notification_url = "http://108.175.14.205/SMISApi/api/smis/subscriptionresponse";
    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    string json = serializer.Serialize(Subscriptiondetails);
}

public class Map
{
    public string username { get; set; }
    public string password { get; set; }
    public string api { get; set; }
    public string msisdn { get; set; }
    public int amount { get; set; }
    public string narration { get; set; }
    public string reference { get; set; }
    public string status_notification_url { get; set; }
}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim Subscriptiondetails As Map
    Subscriptiondetails.username = "5"
    Subscriptiondetails.password = "ty3"
    Subscriptiondetails.api = "depositmobilemoney"
    Subscriptiondetails.msisdn = "Phones.Text"
    Subscriptiondetails.amount = 10000
    Subscriptiondetails.narration = "Subscription"
    Subscriptiondetails.reference = "sbscr"
    Subscriptiondetails.status_notification_url = "http://108.175.14.205/SMISApi/api/smis/subscriptionresponse"
    Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New Script.Serialization.JavaScriptSerializer()
    serializer.Serialize(Subscriptiondetails)
End Sub

Public Class Map
    Public username As String
    Public password As String
    Public api As String
    Public msisdn As String
    Public amount As Integer
    Public narration As String
    Public reference As String
    Public status_notification_url As String
End Class