0

Here is my code.Everything works fine when I hard code message attribute in the json. I want to assign variable in 'message' attribute of the json.

var client = new RestClient("172.16.255.254:2016/settings");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\r\n\r\n\"_id\": 
\"98e8d3cd47fad6ce8e3f7b8d42cb4d9b\",\r\n\r\n\"_type\": 
\"SetRealtimeMessage\" ,\r\n\r\n\"message\": \"c3BlZWQgZXhjZWVkZWQh\", 
\r\n\"width\":240,\r\n\"fontBackground\":0,\r\n\"fontColor\": 
0xFFFFBF00,\r\n//\"fontColor\": 0XFFBF00,\r\n\"fontSize\": 
24,\r\n\r\n\"lineSpace\": 0,\r\n\r\n\"left\": 0, 
\r\n\r\n\"windowBackground\": 0, \r\n\r\n\"verticalPos\": 0, 
\r\n\r\n\"horizontalPos\": 0, \r\n\r\n\r\n\"showType\": 0, \r\n\"speed\": 20, 
\r\n\r\n\"moveCount\": 1,\r\n\r\n\r\n\"residenceTime\": -1\r\n\r\n}",  
ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
1
  • Creating a JSON in this manner is not a good and true way. try creating an object and then by using Newton.Json or ... try to Serialize your JSON Object. Commented Feb 19, 2020 at 12:36

4 Answers 4

3

To avoid writing out a long JSON string manually, you can use the ubiquitous Newtonsoft.Json NuGet package to create a JSON string from an object.

using System;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        var message = "Hello, World!";

        string json = JsonConvert.SerializeObject(new 
        {
            id = 123,
            message = message
        });

        Console.WriteLine(json);

        // {"id":123,"message":"Hello, World!"}
    }       
}

DEMO: https://dotnetfiddle.net/FPFrng

You can pass any object in, I'm just creating an anonymous object here for a simple demo.

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

Comments

2

Use JSON.NET and serialize to json your object and send it.

var myVariable = ...

string message = JsonConvert.SerializeObject(myVariable);
request.AddParameter("application/json", message);

Comments

2

Short and ugly answer :

You can concate your JSON string along with your variable, such as

var MyValue = "bar";
var json = "{\"foo\":\"" + MyValue + "\"}"; // {"foo":"bar"}

Long and better (IMHO) way

Do not directly write JSON strings ! Do you notice how unreadable it is ? You can instead create anonymous objects and serialize them, using, in example, json.net :

var MyValue = "bar";
var MyObject = new
{
    foo = MyValue
};
// using Newtonsoft.Json; // <--- taken from the nugget package newtonsoft.json
var json = JsonConvert.SerializeObject(MyObject, Formatting.Indented);
Console.WriteLine(json);

This outputs :

{
  "foo": "bar"
}

Comments

0

You can use string.Format()

request.AddParameter("application/json", string.Format("{\r\n\r\n\"_id\": 
\"98e8d3cd47fad6ce8e3f7b8d42cb4d9b\",\r\n\r\n\"_type\": 
\"SetRealtimeMessage\" ,\r\n\r\n\"message\": \"{0}\", 
\r\n\"width\":240,\r\n\"fontBackground\":0,\r\n\"fontColor\": 
0xFFFFBF00,\r\n//\"fontColor\": 0XFFBF00,\r\n\"fontSize\": 
24,\r\n\r\n\"lineSpace\": 0,\r\n\r\n\"left\": 0, 
\r\n\r\n\"windowBackground\": 0, \r\n\r\n\"verticalPos\": 0, 
\r\n\r\n\"horizontalPos\": 0, \r\n\r\n\r\n\"showType\": 0, \r\n\"speed\": 20, 
\r\n\r\n\"moveCount\": 1,\r\n\r\n\r\n\"residenceTime\": -1\r\n\r\n}",messageVar)

4 Comments

So you need to make that message property as the parameter, in string.Format(); . But I recommend you to create a JSON Object property as mentioned by Kurt
Thanks. But for this I am getting exception as Format string contains invalid placeholder
Oh sorry that may be because of the {{ to be used while we have to print { string.Format(" {{ \r\n\r\n\"_id\": ...... like this
This is not the right way to create a json like this

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.