murmansk murmansk -3 years ago 128
C# Question

Pass varibale in JSON string in C#

How do I pass my variable in JSON string as shown below?

string name = "john";
string json =@"{
'EmployeeName': name,
'EmployeeID': '123',
}


when I am trying ablove I am facing error?

Answer Source

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',
                    }"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download