[英]How to add A list or array into json body [on hold]
i have to use a webservice, to create user on a portal system. 我必须使用Web服务,才能在门户网站系统上创建用户。 In my case I have a documentation how to provide the correct data for the restApi. 就我而言,我有一个文档,说明如何为restApi提供正确的数据。
here is the documented how to: 这里记录了如何:
Body: 身体:
{
"Titel":"false",
"Dts":[
{
"code":"4712",
"date":"2018-05-01"
}
],
"Info":"Here we are"
}
I tried it with restSharp, but without success. 我用restSharp尝试过,但是没有成功。 Here is my coding. 这是我的编码。
create_user user_value = new create_user();
add_user_dts dts_value = new add_user_dts();
create_user.Titel = "false";
dts_value.code = "4712";
dts_value.date = "2018-05-01";
create_user.DTS = dts_value;
dts_value.Info = "Here we are";
request.AddJsonBody(JsonConvert.SerializeObject(create_user));
But if I ask for the response, i'll get a "Bad Request"! 但是,如果我要求答复,我会收到“错误请求”! I Checkt the serialized Object and found the problem. 我检查了序列化的对象并发现了问题。
{\\"Titel\\":\\"false\\",\\"DTS\\":{\\"code\\":\\"4712\\",\\"date\\":\\"2018-05-01\\"},\\"Info\\":\\"Here we are\\"}
There are missing square brackets!! 缺少方括号!!
{\\"Titel\\":\\"false\\",\\"DTS\\":
[ {\\"code\\":\\"4712\\",\\"date\\":\\"2018-05-01\\"}
] ,\\"Info\\":\\"Here we are\\"}
{\\"Titel\\":\\"false\\",\\"DTS\\":
[ {\\"code\\":\\"4712\\",\\"date\\":\\"2018-05-01\\"}
] ,\\"Info\\":\\"Here we are\\"}
zwo can say me, how to get such brackets? zwo可以说我,如何得到这样的括号? Or how to fill a class or a array/list to serialize in the correct form? 或者如何填充类或数组/列表以正确的形式进行序列化?
I'm bloody new in json programming and in RestSharp. 我是json编程和RestSharp中的新手。
who can help me? 谁能帮我? regards from Germany 来自德国的问候
-=stony007_de=- -= stony007_de =-
In the RestRequest you can set the property JsonSerializer where its possible to define the DateFormatHandling. 在RestRequest中,可以设置属性JsonSerializer,可以在其中定义DateFormatHandling。
I strongly recommend that you use Newtonsoft.Json for serialization. 我强烈建议您使用Newtonsoft.Json进行序列化。
public static RestRequest Create(Uri resource)
{
return new RestRequest(resource) { JsonSerializer = MyJsonSerializer.Default, RequestFormat = DataFormat.Json };
}
In the JsonSerializer.Default you have your default date format. 在JsonSerializer.Default中,您具有默认的日期格式。 Something like: 就像是:
public static MyJsonSerializer Default { get { return new MyJsonSerializer(new Newtonsoft.Json.JsonSerializer() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat }); } }
In MyJsonSerializer class you should define methods to serialize and deserialize objects. 在MyJsonSerializer类中,您应该定义用于序列化和反序列化对象的方法。 This process will use the format that you define. 此过程将使用您定义的格式。
public T Deserialize<T>(IRestResponse response)
{
var content = response.Content;
using (var stringReader = new StringReader(content))
{
using (var jsonTextReader = new JsonTextReader(stringReader))
{
try
{
return Serializer.Deserialize<T>(jsonTextReader);
}
catch (Exception exc) when (exc is JsonSerializationException || exc is JsonReaderException)
{
var exception = JsonConvert.DeserializeObject<RestExceptionDetails>(response.Content);
throw new RestException(exception.Message, exception.ServerStackTrace);
}
}
}
}
public string Serialize(object obj)
{
using (var stringWriter = new StringWriter())
{
using (var jsonTextWriter = new JsonTextWriter(stringWriter))
{
Serializer.Serialize(jsonTextWriter, obj);
return stringWriter.ToString();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.