将正确的JSON作为参数发送到RPC

问题描述

我需要对第三方API进行rpc并发送以下JSON

{
  "jsonrpc":"2.0","id":"number","method":"login.user","params":{
  "login":"string","password":"string"
  }
}

我已经创建了一种制作rcp的方法,但是我无法获取正确的JSON

        public JObject Post()
    {
        object[] a_params = new object[] { "\"login\" : \"test@test.ru\"","\"password\": \"Password\""};

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://test.test.ru/v2.0");

        webRequest.ContentType = "application/json; charset=UTF-8";
        webRequest.Method = "POST";

        JObject joe = new JObject();
        joe["jsonrpc"] = "2.0";
        joe["id"] = 1;
        joe["method"] = "login.user";

        if (a_params != null)
        {
            if (a_params.Length > 0)
            {
                JArray props = new JArray();
                foreach (var p in a_params)
                {
                    props.Add(p);
                }
                joe.Add(new JProperty("params",props));
            }
        }

        string s = JsonConvert.SerializeObject(joe);
        // serialize json for the request
        byte[] byteArray = Encoding.UTF8.GetBytes(s);
        webRequest.ContentLength = byteArray.Length;


        WebResponse webResponse = null;
        try
        {
            using (webResponse = webRequest.GetResponse())
            {
                using (Stream str = webResponse.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(str))
                    {
                        return JsonConvert.DeserializeObject<JObject>(sr.ReadToEnd());
                    }
                }
            }
        }
        catch (WebException webex)
        {

            using (Stream str = webex.Response.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(str))
                {
                    var tempRet = JsonConvert.DeserializeObject<JObject>(sr.ReadToEnd());
                    return tempRet;
                }
            }

        }
        catch (Exception)
        {

            throw;
        }
    }

使用我的代码,我得到以下JSON

{"jsonrpc":"2.0","id":1,"params":["\"login\" : \"v.ermachenkov@mangazeya.ru\"","\"password\": \"AmaYABzP2\""]}

据我了解,我的错误是参数是一个数组([])而不是对象({})。根据我的方法,如何获取正确的json?

解决方法

我纠正了我的错误。代码应为

JObject a_params = new JObject { new JProperty("login","login"),new JProperty("password","Password") };

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://test.test.ru/v2.0");

        webRequest.ContentType = "application/json; charset=UTF-8";
        webRequest.Method = "POST";

        JObject joe = new JObject();
        joe["jsonrpc"] = "2.0";
        joe["id"] = "1";
        joe["method"] = "login.user";
        joe.Add(new JProperty("params",a_params));

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...