0

I'm trying to pass c# variables into the following string and I've got really lost around how to do it.

This is the string without the use of variables:

string data = @"{ ""fields"": { 
    ""project"":
    {
        ""key"": ""TEST""
    },
    ""summary"": ""Test Ticket"",
    ""description"": ""test"",
    ""issuetype"": {""name"": ""test""},
    ""assignee"": { ""name"": ""test""}
}}";

Then when I try to include a variable (test.Text is a asp textbox) I do it this way:

string data = @"{ ""fields"": { 
    ""project"":
    {
        ""key"": ""TEST""
    },
    ""summary"": ""Test Ticket"",
    ""description"": """ + test.Text + """,
    ""issuetype"": {""name"": ""test""},
    ""assignee"": { ""name"": ""test"" }
}}";

But that isn't working. Is there a different way to include variable data in there?

When I try and build it then it says

} expected.

So I went through and tried to wrap each one in } like this, but it didn't help :(

string data = @"{ ""fields"": { 
    ""project"":
    {
        ""key"": ""TEST""
    },
    ""summary"": ""Test Ticket"",
    ""description"": {""" + test.Text + """},
    ""issuetype"": {""name"": ""test""},
    ""assignee"": { ""name"": ""test"" }
}}";

Thanks!

5
  • 3
    What specifically "isn't working" about it? Commented Apr 28, 2021 at 13:29
  • 13
    Don't build JSON like that. All kinds of fun (cough, cough) is going to happen once test.Text has quotes etc in it. Commented Apr 28, 2021 at 13:31
  • 4
    instead of manipulating strings in order to build valid JSON you should definitly use a serializer and let it write proper JSON, e.g. JsonConvert. Commented Apr 28, 2021 at 13:33
  • 6
    The code doesn't compile because the second literal string isn't verbatim, it would have to be + test.Text + @"""},, but as have been said, the proper way to build json is not to do it with string concatenation, use a proper Json library for this. Commented Apr 28, 2021 at 13:35
  • You might want to change the title of this question to say something like, "how to create a JSON object in C#". In my personal opinion, it better represents what you are asking. Commented Apr 28, 2021 at 14:09

3 Answers 3

3

If you're trying to make a JSON object. You are better off creating a class that is a blueprint for the JSON object. Then use NewtonSoft to convert to a JSON string.


namespace example
{
    public class Ticket
    {
        public int Id { get; set; }
        public string Description { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Ticket tic = new Ticket()
            {
                Id = 123,
                Description = "hello motto"
            };

            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(tic));
            Console.ReadLine();
        }
    }
}

yields the results

{"Id":123,"Description":"hello motto"}

Also note that you can put classes in classe and it will nest it correctly.

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

Comments

0

you can use string interpolation in order to achieve that, but much better (and less error prone) would be using some JSON related classes like JObject from Newtonsoft or the JSON.NET package.

For example, this is how you would add a variable to a JObject:

JObject data = new JObject()
{ 
    {"description", test.Text}
};

Cheers

Comments

-1

I concur with the other commenters, that this is a clunky and very unmaintainable way to build up a JSON string. This is an excellent library to use instead: Newtonsoft JSON

If you must do it this way for some reason, I suggest using the much improved string interpolation which was made available starting in C#6.

Here is how that would look:

string data = $@"{{ ""fields"": {{ 
    ""project"":
    {{
        ""key"": ""TEST""
    }},
    ""summary"": ""Test Ticket"",
    ""description"": ""{test.Text}"",
    ""issuetype"": {{""name"": ""test""}},
    ""assignee"": {{ ""name"": ""test""}}
}}";

Note: This is technically an "interpolated verbatim string" (notice the $@) so now the brackets need to be escaped.

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.