Reputation: 2513
I'm really struggling with serializing some JSON data, the end result needs to look like this:
{
"jsonrpc":"2.0",
"method":"user.authenticate",
"params":{
"user":"<login>",
"password":"<password>"
},
"id":2
}
I'm trying to make use of JSON.NET, i'm passing the data in via a method but i just can't get my head around it. Any idea's on how i can get started?
thanks
Upvotes: 2
Views: 246
Reputation: 1499770
There may well be better ways of doing it, but this seems to work:
using Newtonsoft.Json.Linq;
using System;
class Test
{
static void Main()
{
string json = ConvertToJson("jon", "secret");
Console.WriteLine(json);
}
static string ConvertToJson(string login, string password)
{
JObject container = new JObject();
container["jsonrpc"] = "2.0";
container["method"] = "user.authenticate";
container["id"] = 2;
JObject p = new JObject();
p["user"] = login;
p["password"] = password;
container["params"] = p;
return container.ToString();
}
}
Upvotes: 3