다음 JSON이 포함 된 서버에 요청을 게시해야합니다. 다음과 같은 요청을 나타내는 클래스에 DataContractJsonSerializer 및 DataContract, DataMember 특성을 사용하고 싶습니다.
{"method":"mymethod","parameters":[10,"somestring"]}
이것은 RPC 호출을 나타냅니다.
mymethod(10,"somestring").
일부 API에서. API에는 매개 변수 목록이 다른 많은 호출이 있습니다.
매개 변수 목록에 generic을 사용할 수있는 T 유형의 객체가 포함되어 List<T>
있지만 API에 다른 유형의 매개 변수 목록 (기본이 아닌 객체 포함)이 필요한 경우 이는 간단합니다 .
그렇다면 매개 변수 배열에 대한 DataContract를 어떻게 구성 할 수 있습니까?
고마워 루카스. 따라서 복잡한 유형을 포함하도록 질문을 약간 다시 설명합니다.
{ "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();
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다