谢谢卢卡斯。所以稍微重述问题以包括复杂类型:-
{"method":"mymethod","parameters":[10,"somestring",{SomeProperty:value}]}
这表示对 JSON RPC 调用 mymethod(int,string,ComplexProperty) 的调用
The code is:-
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
namespace JSONConsoleApplication4
{
class Program
{
[DataContract]
public class ComplexType
{
[DataMember]
public string SomeProperty { get; set; }
}
[DataContract]
public class GenericRequest
{
[DataMember]
public string method { get; set; }
[DataMember]
public object[] parameters { get; set; }
}
static void Main(string[] args)
{
MemoryStream ms = new MemoryStream();
DataContractJsonSerializerSettings settings =
new DataContractJsonSerializerSettings() { EmitTypeInformation=EmitTypeInformation.Never,
KnownTypes=new Type[] { typeof(ComplexType) } };
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GenericRequest),settings);
serializer.WriteObject(ms,
new GenericRequest() { method = "mymethod",
parameters = new object[] { 10, "somestring", new ComplexType() { SomeProperty="value"}} });
ms.Position = 0;
string v = new StreamReader(ms).ReadToEnd();
}
}
}