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:
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:
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"
}