0

I get a System.Format exception when trying this:

var jsonString = String.Format( @"{
    ""searchOptions"": {
        ""departurePosition"": { ""id"": {0} },
        ""arrivalPosition"": { ""id"": 376422 },
        ""travelModes"": [ ""Flight"", ""Train"", ""Bus"" ],
        ""departureDate"": ""2017-01-15"",
        ""passengers"": [
          {
            ""age"": 12,
            ""discountCards"": [ ]
          }
        ],
        ""userInfo"": {
          ""identifier"": ""0.jhvlf8amtgk"",
          ""domain"": "".com"",
          ""locale"": ""en"",
          ""currency"": ""EUR""
        },
        ""abTestParameters"": [ ]
    }
}", departurePosition);

I need this when sending a post request to a server.

How can I solve this problem ?

2
  • 2
    Create a class which will represent the data and use var json = Newtonsoft.Json.JsonConvert.SerializeObject(yourDataInstance) to create a json string. Commented Jan 15, 2017 at 8:01
  • As @Fabio suggested, the best option to create a maintainable solution for your Json output would be to create classes an use a library like Newtonsoft to create a valid output. As an example, I created a small demo here Commented Jan 15, 2017 at 8:48

2 Answers 2

2

It's probably because the use of { and } To Escape the { and } use {{ and }}

Sign up to request clarification or add additional context in comments.

Comments

0

OfirW already shared this, string.Format() giving “Input string is not in correct format”

If this is just one variable, regular string concatenation will work fine.

var jsonString =  
@"{ ""searchOptions"": {
        ""departurePosition"": { ""id"": " + departurePosition + @"},
        ""arrivalPosition"": { ""id"": 376422 },
        ""travelModes"": [ ""Flight"", ""Train"", ""Bus"" ],
        ""departureDate"": ""2017-01-15"",
        ""passengers"": [
          {
            ""age"": 12,
            ""discountCards"": [ ]
          }
        ],
        ""userInfo"": {
          ""identifier"": ""0.jhvlf8amtgk"",
          ""domain"": "".com"",
          ""locale"": ""en"",
          ""currency"": ""EUR""
        },
        ""abTestParameters"": [ ]
  }
}";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.