简体   繁体   English

序列化C#中的JSON包含

[英]Serialize an include for JSON in C#

I have tried countless iterations of this to get it to work. 我已经尝试了无数次迭代来使其工作。 I am trying to serialize a request that has an include in it. 我正在尝试序列化包含其中的请求。 The string 'param' is a variable. 字符串“ param”是一个变量。 The request looks like this: 该请求看起来像这样:

{"json-rpc":"2.0","method":"getThings","params":{"guId":"All"},"id":1,"version":"1.0"} {“ json-rpc”:“ 2.0”,“ method”:“ getThings”,“ params”:{“ guId”:“ All”},“ id”:1,“ version”:“ 1.0”}

The nested guId portion is stumping me. 嵌套的guId部分让我很沮丧。 This is in .NET 3.5 CF thus the Newtonsoft usage. 这是在.NET 3.5 CF中,因此是Newtonsoft的用法。

This is the closest I was able to get: 这是我能得到的最接近的:

using Newtonsoft.Json

BuildRequest jRequest = new BuildRequest
{
    JsonRpc = "2.0",
    Method = "getThings",
    Params = param,
    Id = 1,
    Version = "1.0",
};

var httpRequest = JsonConvert.SerializeObject(jRequest, Formatting.Indented);

private class ParamsP
{
    [JsonProperty("guId")]
    public string GroupId { get; set; }
}

private class BuildRequest
{
    [JsonProperty("json-rpc")]
    public string JsonRpc { get; set; }

    [JsonProperty("method")]
    public string Method { get; set; }

    [JsonProperty("params")]
    public ParamsP Params { get; set; }

    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("version")]
    public string Version { get; set; }
}

I am getting 我正进入(状态

Cannot implicitly convert string to ParamsP. 无法将字符串隐式转换为ParamsP。

so i was able to get this to work in a console project using .NET 3.5 CF feel like you might be missing something elsewhere or perhaps I am confused on the question creating an object of type BuildRequest and the class being called type BuildGetPlayers 因此,我能够使用.NET 3.5 CF在控制台项目中使用它,就像您可能在其他地方缺少某些东西,或者我对创建BuildRequest类型的对象以及名为BuildGetPlayers类型的类的问题感到困惑

public class Process
{
    private class ParamsP
    {
        [JsonProperty("guId")]
        public string GroupId { get; set; }
    }

    private class BuildGetPlayers
    {
        [JsonProperty("json-rpc")]
        public string JsonRpc { get; set; }

        [JsonProperty("method")]
        public string Method { get; set; }

        [JsonProperty("params")]
        public ParamsP Params { get; set; }

        [JsonProperty("id")]
        public int Id { get; set; }

        [JsonProperty("version")]
        public string Version { get; set; }
    }


    BuildGetPlayers jRequest = new BuildGetPlayers
    {
        JsonRpc = "2.0",
        Method = "getThings",
        Params = new ParamsP
        {
            GroupId = "something"
        },
        Id = 1,
        Version = "1.0",
    };

    public string Get()
    {
        var httpRequest = JsonConvert.SerializeObject(jRequest, Formatting.Indented);
        return httpRequest;
        /* returns {
              "json-rpc": "2.0",
              "method": "getThings",
              "params": {
                "guId": "something"
              },
              "id": 1,
              "version": "1.0"
            }
        */
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM