[英]WCF REST web service - Deserialization of Json
I am encountering a problem on my WCF web service. 我在WCF Web服务上遇到问题。 I have this error:
我有这个错误:
The server encountered an error processing the request.
服务器在处理请求时遇到错误。
I think is due to the deserialization of the JSON, I will past my code, please give me some help, thank you. 我认为是由于JSON的反序列化,我将过去我的代码,请给我一些帮助,谢谢。
Here is the raw JSON: 这是原始的JSON:
{
"properties" : {
"callID" : "4A79825AE0914B6B9B27F477CAF8A32B",
"timestamp" : "2018-06-05T08:50:41.064+0000",
"data" : {
"Name" : "Y",
"Surname" : "X",
"Age" : [ "25" ]
},
"localEvent" : "X",
"eventtype" : "X"
}
}
And i have the following classes: 我有以下课程:
public class CSModel
{
public CSProperties properties { get; set; }
}
public class CSProperties
{
public string callID{ get; set; }
public string timestamp{ get; set; }
public CSData DataModel { get; set; }
public string localEvent{ get; set; }
public string eventtype{ get; set; }
}
public class CSData
{
public string Name{ get; set; }
public string Surname{ get; set; }
public string Age{ get; set; }
}
And here i decode my JSON: 在这里,我解码了JSON:
string jsonBody = new StreamReader(contents).ReadToEnd();
CSModel jsonModel = JsonConvert.DeserializeObject<CSModel>(jsonBody);
There are couple of things which you need to change, since your C# class representation isn't correct 由于您的C#类表示形式不正确,您需要更改几件事
Use attributes ( [JsonProperty(ProperyName = "data")]
) to decorate class properties which have different name than the name in the JSON itself. 使用属性(
[JsonProperty(ProperyName = "data")]
)装饰名称与JSON本身名称不同的类属性。 If you don't do that, your DataModel
property will remain null. 如果您不这样做,那么您的
DataModel
属性将保持为null。
"Age"
property holds value of string array type, which means you need to change C# property into string[]
. "Age"
属性保存字符串数组类型的值,这意味着您需要将C#属性更改为string[]
。 This must be changed to prevent exceptions due to type mismatch 必须更改此项以防止由于类型不匹配而导致的异常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.