I am writing a program in json-rpc and I want to declare string variable in json string i am doing mistake while declaring it dont know what is the correct format for that is there anybody who can solve this problem.
(string amnt= "1000000" I want to declare it in json amount in single quotes)
string amnt = "100000000";
string json = @"
{
'method': 'submit',
'params': [{
'secret': 'snL7AcZbKsHm1H7VjeZg7gNS55Xkd',
'tx_json': {
'Account': 'rHSqhmuevNJg9ZYpspYHNnQDxraozuCk5p',
'TransactionType': 'PaymentChannelCreate',
'Amount': '"+amnt+"',
'Destination': 'rD6CGd2uL9DZUVDNghMqAfr8doTzKbEtGA',
'SettleDelay': 86400,
'PublicKey': '023693F15967AE357D0327974AD46FE3C127113B1110D6044FD41E723689F81CC6',
'DestinationTag': 20170428
},
'fee_mult_max': 1000
}]
}";
The immediate problem is that you've got two string literals: a verbatim string literal at the start, and then a regular string literal after the string concatenation with amnt. To make it simpler to see, you've got:
string text = @"some text
more text" + amnt + "more text
more text";
That second string literal is a regular string literal, which means it can't go over multiple lines. That's why you're getting an error at the moment.
That's not the only problem you've got though:
There are several options here:
{amnt} to include the value there. The disadvantage is that you'd need to double all the braces to indicate that you wanted actual braces@ at the start of itI'd definitely take the last option - I'd use Json.NET.
There are lots of ways of doing this in Json.NET. For example:
JObject and JArray instances.Here's an example of the latter approach:
using System;
using Newtonsoft.Json;
class Test
{
public static void Main()
{
string amount = "1000000";
var obj = new
{
method = "submit",
// Note: @ is required as params is a keyword
@params = new[]
{
new
{
secret = "snL7AcZbKsHm1H7VjeZg7gNS55Xkd",
tx_json = new
{
Account = "rHSqhmuevNJg9ZYpspYHNnQDxraozuCk5p",
TransactionType = "PaymentChannelCreate",
Amount = amount,
Destination = "rD6CGd2uL9DZUVDNghMqAfr8doTzKbEtGA",
SettleDelay = 86400,
PublicKey = "023693F15967AE357D0327974AD46FE3C127113B1110D6044FD41E723689F81CC6",
DestinationTag = 20170428
},
fee_mult_max = 1000
}
}
};
string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
Console.WriteLine(json);
}
}
Output:
{
"method": "submit",
"params": [
{
"secret": "snL7AcZbKsHm1H7VjeZg7gNS55Xkd",
"tx_json": {
"Account": "rHSqhmuevNJg9ZYpspYHNnQDxraozuCk5p",
"TransactionType": "PaymentChannelCreate",
"Amount": "1000000",
"Destination": "rD6CGd2uL9DZUVDNghMqAfr8doTzKbEtGA",
"SettleDelay": 86400,
"PublicKey": "023693F15967AE357D0327974AD46FE3C127113B1110D6044FD41E723689F81CC6",
"DestinationTag": 20170428
},
"fee_mult_max": 1000
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With