简体   繁体   English

C#中的Json数组反序列化问题

[英]Json array Deserialization Issue in C#

I have a Web API method to mapping a Json array to C# object.我有一个 Web API 方法来将 Json 数组映射到 C# 对象。

My parameter should be like the following:我的参数应该如下所示:

[{"event":"inbound","msg":"TestMEssage","ts":123456}]

and my code is我的代码是

var events = JsonConvert.DeserializeObject<IEnumerable<MailEvent>>(mandrill_inbound);
public class MailEvent
    {

        [JsonProperty(PropertyName = "event")]
        public string Event { get; set; }

        [JsonProperty(PropertyName = "msg")]
        public string Msg { get; set; }

        [JsonProperty(PropertyName = "ts")]
        public string TimeStamp { get; set; }
    }

But when am trying to Deserialize the JSON it's showing exception.但是当我尝试反序列化 JSON 时,它显示异常。

But If I am trying to set a variable inside the method like但是如果我试图在方法中设置一个变量,比如

Public const string mandrill_inbound = @"[
    {
        ""event"": ""inbound"",
        ""msg"": ""tewst"",
        ""ts"": 1368214102
    },
    {
        ""event"": ""inbound"",
        ""msg"": ""test2"",
        ""ts"": 1368214102
    }
]";

This code will be working perfectly for me.这段代码对我来说很完美。

Noting that it has starting with @"" and all the key values are surrounded with multiple Double quotes( ""event"" ).请注意,它以@""开头,并且所有键值都用多个双引号 ( ""event"" ) 括起来。

Actually what is the difference between these two formats.其实这两种格式有什么区别。

Any help will be appreciated.任何帮助将不胜感激。

This is literally the same string without needing to escape anything.这实际上是相同的字符串,无需转义任何内容。

That is the @ indicates that the string is declared as is, ignoring any and all characters that in a normal string would indicate some escape.那就是@表示字符串按原样声明,忽略在正常字符串中表示某种转义的任何和所有字符。

The only exception is the double arrow Quote.唯一的例外是双箭头报价。 The double quote needs to be escaped when the string is prefixed by @.当字符串以@ 为前缀时,需要对双引号进行转义。 The way to do this, is by using ""这样做的方法是使用""

So the same string without @ would be所以没有@的相同字符串将是

Public const string mandrill_inbound = "[\n" +
"{\n" +
    "\"event\": \"inbound\",\n" +
    "\"msg\": \"tewst\",\n" +
    "\"ts\": 1368214102\n" +
"},\n" +
"{\n" +
    "\"event\": \"inbound\",\n" +
    "\"msg\": \"test2\",\n" +
    "\"ts\": 1368214102\n" +
"}]";

The string above should also work.上面的字符串也应该有效。 What is the exception you are getting?你得到的例外是什么?

Finally, I found the Issue,最后,我找到了问题,

It appears that the JSON I receive has been serialized twice, No idea from where this happened.The first double-quote might be added by your debugger, but the second (the escaped \\" one) really appears to be part of the data you're processing. The error message also makes sense this way, it deserializes a string and then attempts to cast it to an ApiResult.看来我收到的 JSON 已被序列化两次,不知道这是从哪里发生的。第一个双引号可能是您的调试器添加的,但第二个(转义的\\" )似乎确实是您的数据的一部分'正在处理。错误消息也以这种方式有意义,它反序列化一个字符串,然后尝试将其转换为 ApiResult。

"\"{\\"event\\":\\"Inbound\\",\\"msg\\":...

Try deserializing the data as a string and then deserializing its result to an ApiResult尝试将数据反序列化为字符串,然后将其结果反序列化为 ApiResult

   var content = response.Content;              
   var jsonResult = JsonConvert.DeserializeObject(content).ToString();
   var result= JsonConvert.DeserializeObject<Model>(jsonResult);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM