[Solved] Add variable to json string in c#


Your question is not clear enough. But if I understand you correctly you want to convert this json

{
   "fields":{
      "ID": ID,
      "Device Name": deviceName
   }
}

into string.

I strongly suggest you use serialization libraries and do not write json yourself. You can easily create a class or dictionary and serialize it to that json.

But if you insist you have two errors in your json string.

1- After deviceName, you used ' instead of ".

2- You add extra " before last }.

So the correct json should be something like this

string addItemJsonString = "{\"fields\":{\"ID\":\"" + ID + "\",\"DeviceName\":\"" + deviceName + "\"}}";

solved Add variable to json string in c#