在C#中序列化JSON的include
本文关键字:include JSON 序列化 | 更新日期: 2023-09-27 17:59:02
我已经尝试了无数次迭代来让它发挥作用。我正在尝试序列化一个包含的请求。字符串"param"是一个变量。请求如下:
{"json-rpc":"2.0","method":"getThings","params":{"guId":"All"},"id":1,"version":"1.0"}
嵌套的guId部分让我很困惑。这是在.NET 3.5 CF中,因此是Newtonsoft的用法。
这是我能得到的最接近的:
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; }
}
我得到
无法将字符串隐式转换为ParamsP。
所以我能够在使用.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"
}
*/
}
}