Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want make a json string like this and post in asp.net c#
{
  "data": {
"amount":100,"currencyType":"MYR","expiry":{"type":"PERMANENT"},"isPreFillAmount":true,"method":null,"order":{"detail":"detail","title":"title"},"redirectUrl":"https://google.com","storeId":"1234","type":"DYNAMIC"
  },
  "method": "post",
  "nonceStr": "MNhrdDgIDlTKdlYXzfhEvqHpGjRhvrPe",
  "privateKey": "123456",
  "requestUrl": "https://sb-open.revenuemonster.my/v3/payment/transaction/qrcode",
  "signType": "sha256",
  "timestamp": "1532069238"
}



.. Before i use like below it worked but now the problem is how to make the json string where data have like array inside have expiry and orders have data in bracket





Appreciate any help

What I have tried:

StreamWriter(httpWebRequest.GetRequestStream()))
{
    string jsonsign1 = new JavaScriptSerializer().Serialize(new
    {


        data = "{" + "amount" + ":" + "100" + "," + "currencyType" + ":" + "MYR" + "}",
        method = "post",
        nonceStr = nonce,
        privateKey = "123",
        requestUrl = "https://sb-open.revenuemonster.my/v3/payment/transaction/qrcode",
        signType = "sha256",
        timestamp = timestampStr

    });

    streamWriter.Write(jsonsign1);
}
Posted
Updated 20-Dec-18 15:44pm

C#
string jsonsign1 = new JavaScriptSerializer().Serialize(new
{
    data = new
    {
        amount = 100,
        currencyType = "MYR",
        expiry = new { type = "PERMANENT" },
        isPreFillAmount = true,
        order = new 
        { 
            detail = "detail",
            title = "title"
        },
        redirectUrl = "https://google.com",
        storeId = "1234",
        type = "DYNAMIC"
    },
    method = "post",
    nonceStr = nonce,
    privateKey = "123",
    requestUrl = "https://sb-open.revenuemonster.my/v3/payment/transaction/qrcode",
    signType = "sha256",
    timestamp = timestampStr
});
 
Share this answer
 
v2
Comments
AtulSharma609 20-Dec-18 22:22pm    
Thank you so much, sir it's really works
I'd suggest you to create a concrete class for each object that you want to return as an output instead of relying on anonymous types. This way you could easily modify the object that you want to add/remove and it's easy to maintain it. For example you could create the following class:

C#
public class Response{
    public Data Data { get; set; }
    public string Method { get; set; }
    public string NonceStr { get; set; }
    public string PrivateKey { get; set; }
    public string RequestUrl { get; set; }
    public string SignType { get; set; }
    public DateTime TimeStamp { get; set; }
}

public class Data{
    public decimal Amount { get; set; }
    public string CurrencyType { get; set; }
    public Expiry Expiry { get; set; }
    public bool IsPreFillAmount { get; set;}
    public Order Order { get; set;}
    public string RedirectUrl { get; set;}
    public string StoreId { get; set;}
    public string Type { get; set;}
}

public class Order{
    public string Detail { get; set;}
    public string Title { get; set;}
}

public class Expiry{
    public string Type { get; set;}
}



You then use Newtonsoft.Json to serialize the object as JSON string like this:

C#
 Response data = new Response(){
				Data = new Data(){
					Amount = 100,
        			CurrencyType = "MYR",
        			Expiry = new Expiry() { Type = "PERMANENT" },
        			IsPreFillAmount = true,
        			Order = new Order() 
        			{ 
            			Detail = "detail",
            			Title = "title"
        			},
        			RedirectUrl = "https://google.com",
        			StoreId = "1234",
        			Type = "DYNAMIC"
				},
				Method = "Post",
				NonceStr = "nonce",
    			PrivateKey = "123",
    			RequestUrl = "https://sb-open.revenuemonster.my/v3/payment/transaction/qrcode",
    			SignType = "sha256",
    			TimeStamp = DateTime.Now
};
			
string jsonResponse = JsonConvert.SerializeObject(data);



The variable jsonResponse should return something like this:
{
  "Data": {
    "Amount": 100,
    "CurrencyType": "MYR",
    "Expiry": {
      "Type": "PERMANENT"
    },
    "IsPreFillAmount": true,
    "Order": {
      "Detail": "detail",
      "Title": "title"
    },
    "RedirectUrl": "https://google.com",
    "StoreId": "1234",
    "Type": "DYNAMIC"
  },
  "Method": "Post",
  "NonceStr": "nonce",
  "PrivateKey": "123",
  "RequestUrl": "https://sb-open.revenuemonster.my/v3/payment/transaction/qrcode",
  "SignType": "sha256",
  "TimeStamp": "2018-12-21T02:37:17.5973561+00:00"
}
 
Share this answer
 
Comments
AtulSharma609 25-Dec-18 22:08pm    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900