I want create a customized JSON string like this:
{"service1":"hello"}
( I simplified the example. In reality the required JSON is more complex. But to explain the problem, this example is fine )
My problem is that the service name "service1" is contained in a variable This is my code:
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json;
public static string CreateCustomJSON(string serviceName, object value)
{
var v = new { serviceName = value };
string json = JsonConvert.SerializeObject(v);
Console.WriteLine(json);
return json;
}
CreateCustomJSON("service1", "hello");
CreateCustomJSON("service2", "John");
CreateCustomJSON("service3", 13);
I got this result:
{"serviceName":"hello"}
{"serviceName":"John"}
{"serviceName":13}
because i do not know how to use Anonymous Types properly
The error is in this line:
var v = new { serviceName = value };
Or maybe there is another way to follow, to build a customized json string
Can you help me?
