How do I pass my variable in JSON string as shown below?
string name = "john";
string json = @"{
'EmployeeName': name,
'EmployeeID': '123',
}
When I try the above I get an error.
Well you can concate the variable like
string json =@"{
'EmployeeName':" + name +",
'EmployeeID': '123',
}"
You can as well consider using string.Format() for this purpose and in C# 6 you can use variable interpolation syntax like
string json =$"{
'EmployeeName': {name} ,
'EmployeeID': '123',
}"
Here's an example the works for me:
string json = @"[ { 'email': '" + name + "' } ]";
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