How to add json string as application parameter value in configuration file

I have a requirement where I need to get a set of jsons before calling the API. I plan to add these json lines to app.config as shown below.

<add key="Jsons" value="{""Id"":""25"",""Name"":""Value-1""}"/> 

However, adding this result results in a "Missing Spaces" compilation error at the beginning of the value. Please let me know if I missed something. I do not want to create a separate text file for reading jsons. That is why I decided to use app.config myself

+6
source share
3 answers

Your quotation marks are incorrectly formatted. Can you try this:

<add key="Jsons" value='{"Id":"25","Name":"Value-1"}'/>

+5
source

An app.config is still XML! For quotes, you need to use an XML escape sequence.

 <add key="Jsons" value="{&quot;Id&quot;:&quot;25&quot;,&quot;Name&quot;:&quot;Value-1&quot;}"/> 
+2
source

Here I see two options:

  • Use "\" to exit:

      <add key="Jsons" value="{\"Id\":\"25\",\"Name\":\"Value-1\"}"/> 
  • Use a single quote:

     <add key="Jsons" value="{'Id':'25','Name':'Value-1'}"/> 
0
source

All Articles