开发者

How can I deserialize elementary types from json in ASP.NET(System.Runtime.Serialization.Json)

I have a little problem. When I'm using DataContractJsonSerializer with complex types(own types) it works fine. But I have to deserialize TimeStamp or DateTime from a string. So I cant mark these types with DataContract, DataMember attributes.

I wrote some code

string jsonedTS = "PT2M15S";

DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(TimeSpan));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonedTS));
try {
    result.Takes = (TimeSpan) jsonSerializer.ReadObject(ms);
} catch {
 ;
}

And I catch this Exception

{"There was an error deserializing the object of type System.TimeSpan. Encountered unexpected character 'P'."} System.Exception {System.Runtime.Serialization.SerializationException}

And My Question IS How Can I deserialize开发者_StackOverflow社区


You can try with Json.Net library - it's worked pretty well for us in the past.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜