Newtonsoft.Json 未按预期序列化对象 - 错误 400

我正在尝试发送带有我创建的对象的 json 字符串的 post 请求。然而,在序列化我的 C# 对象并发布后,我收到 400 错误(解析 JSON 时出现问题)


我正在使用 Newtonsoft.Json dll 来序列化我的对象。这是我正在序列化的对象:


public class CreateRepository

{

    [JsonProperty("name")]


    public string Name { get; set; }


    [JsonProperty("description")]


    public string Description { get; set; }


    [JsonProperty("homepage")]


    public object Homepage { get; set; }


    [JsonProperty("gitignore_template")]

    public string GitIgnoreTemplate { get; set; }


    [JsonProperty("license_template")]

    public string LicenceTemplate { get; set; }


    [JsonProperty("private")]


    public bool Private { get; set; }


    [JsonProperty("has_projects")]

    public bool HasProjects { get; set; }


    [JsonProperty("has_issues")]

    public bool HasIssues { get; set; }


    [JsonProperty("has_template")]

    public bool HasTemplate { get; set; }


    [JsonProperty("has_wiki")]

    public bool HasWiki { get; set; }

}

然后我像这样序列化对象的实例:


var content = JsonConvert.SerializeObject(repository);

然后生成下面的 json 字符串:


{

    \"name\": \"Test\",

    \"description\":null,

    \"homepage\":null,

    \"gitignore_template\":null,

    \"license_template\":null,

    \"private\":false,

    \"has_projects\":false,

    \"has_issues\":false,

    \"has_template\":false,

    \"has_wiki\":false}

}

尝试发布请求后,我得到以下信息:


{

    "message": "Problems parsing JSON",

    "documentation_url": "https://developer.github.com/v3/repos/#create"

}

有谁知道为什么我的对象以这种方式序列化?


更新:


我可以使用 JSON.Net 反序列化该对象,不会出现任何错误。


我发布的网址如下:


https://api.github.com/user/repos

这就是我发送请求的方式:


var response = await _httpClient.PostAsJsonAsync("user/repos", content);


一只斗牛犬
浏览 78回答 1
1回答

www说

问题是“has_template”在 Github 端是未知的,可能应该是“is_template”。请参阅您在请求响应中返回的链接以获取正确的参数名称。因此,您没有在类中使用正确的属性名称/名称属性。序列化不是问题。
打开App,查看更多内容
随时随地看视频慕课网APP