JSON序列化错误错误请求

时间:2016-02-25 01:34:16

标签: c# json.net httpwebrequest

我正在使用Newtonsoft尝试序列化一些JSON来执行HttpWebRequest POST

我一直在回复说“错误请求”

我假设我的JSON格式不正确。以下是我的代码

任何帮助非常感谢

Account account = new Account();
account.Name = "TESTACCOUNT";

var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://app01.nutshell.com/api/v1/json");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
httpWebRequest.Credentials = new NetworkCredential("username", "password");

var serializer = new JsonSerializer();

using (var tw = new Newtonsoft.Json.JsonTextWriter(streamWriter))
{

    serializer.Serialize(tw,
                 new
                 {
                   method = "newAccount",
                   @params = account                                      
                  });
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
}

1 个答案:

答案 0 :(得分:1)

如果它只是JSON:

{
    "method": "newAccount",
    "@params": "account"
}

http://pro.jsonlint.com/