Shaiju Janardhanan
Shaiju Janardhanan

Reputation: 564

How to add a json string as a app setting value in config file

I have a requirement whereby I need to fetch a set of jsons before making a API call. I am planning to add these json strings in app.config as shown below

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

However adding this results in a compilation error "Missing whitespace" at the start of the value. Please let me know if i am missing something. I dont want to create a separate text file to read jsons from. Thats why i decided to use app.config itself

Upvotes: 7

Views: 13226

Answers (3)

Louie Almeda
Louie Almeda

Reputation: 5632

Your quotes are not correctly formatted. Can you try this:

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

Upvotes: 13

Kevin Burdett
Kevin Burdett

Reputation: 2982

An app.config is still XML! You need to use the XML escape sequence for quotes.

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

Upvotes: 2

alexandergs
alexandergs

Reputation: 192

I see two choices here:

  1. Use "\" to escape:

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

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

Upvotes: 0

Related Questions