<small id='i5gsB'></small><noframes id='i5gsB'>

  1. <tfoot id='i5gsB'></tfoot>
  2. <i id='i5gsB'><tr id='i5gsB'><dt id='i5gsB'><q id='i5gsB'><span id='i5gsB'><b id='i5gsB'><form id='i5gsB'><ins id='i5gsB'></ins><ul id='i5gsB'></ul><sub id='i5gsB'></sub></form><legend id='i5gsB'></legend><bdo id='i5gsB'><pre id='i5gsB'><center id='i5gsB'></center></pre></bdo></b><th id='i5gsB'></th></span></q></dt></tr></i><div id='i5gsB'><tfoot id='i5gsB'></tfoot><dl id='i5gsB'><fieldset id='i5gsB'></fieldset></dl></div>

      • <bdo id='i5gsB'></bdo><ul id='i5gsB'></ul>
    1. <legend id='i5gsB'><style id='i5gsB'><dir id='i5gsB'><q id='i5gsB'></q></dir></style></legend>

      有时是数组有时是对象时反序列化JSON

      时间:2023-08-23

    2. <small id='S6SzJ'></small><noframes id='S6SzJ'>

        1. <legend id='S6SzJ'><style id='S6SzJ'><dir id='S6SzJ'><q id='S6SzJ'></q></dir></style></legend>

          <i id='S6SzJ'><tr id='S6SzJ'><dt id='S6SzJ'><q id='S6SzJ'><span id='S6SzJ'><b id='S6SzJ'><form id='S6SzJ'><ins id='S6SzJ'></ins><ul id='S6SzJ'></ul><sub id='S6SzJ'></sub></form><legend id='S6SzJ'></legend><bdo id='S6SzJ'><pre id='S6SzJ'><center id='S6SzJ'></center></pre></bdo></b><th id='S6SzJ'></th></span></q></dt></tr></i><div id='S6SzJ'><tfoot id='S6SzJ'></tfoot><dl id='S6SzJ'><fieldset id='S6SzJ'></fieldset></dl></div>
          <tfoot id='S6SzJ'></tfoot>
            <tbody id='S6SzJ'></tbody>

              <bdo id='S6SzJ'></bdo><ul id='S6SzJ'></ul>
                本文介绍了有时是数组有时是对象时反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我在使用 JSON.NET 库反序列化从 Facebook 返回的数据时遇到了一些麻烦.

                I'm having a bit of trouble deserializing data returned from Facebook using the JSON.NET libraries.

                从一个简单的墙贴返回的 JSON 如下所示:

                The JSON returned from just a simple wall post looks like:

                {
                    "attachment":{"description":""},
                    "permalink":"http://www.facebook.com/permalink.php?story_fbid=123456789"
                }
                

                为照片返回的 JSON 如下所示:

                The JSON returned for a photo looks like:

                "attachment":{
                        "media":[
                            {
                                "href":"http://www.facebook.com/photo.php?fbid=12345",
                                "alt":"",
                                "type":"photo",
                                "src":"http://photos-b.ak.fbcdn.net/hphotos-ak-ash1/12345_s.jpg",
                                "photo":{"aid":"1234","pid":"1234","fbid":"1234","owner":"1234","index":"12","width":"720","height":"482"}}
                        ],
                

                一切都很好,我没有问题.我现在遇到了一个来自移动客户端的简单墙贴,其中包含以下 JSON,而反序列化现在因这一个帖子而失败:

                Everything works great and I have no problems. I've now come across a simple wall post from a mobile client with the following JSON, and deserialization now fails with this one single post:

                "attachment":
                    {
                        "media":{},
                        "name":"",
                        "caption":"",
                        "description":"",
                        "properties":{},
                        "icon":"http://www.facebook.com/images/icons/mobile_app.gif",
                        "fb_object_type":""
                    },
                "permalink":"http://www.facebook.com/1234"
                

                这是我反序列化的类:

                public class FacebookAttachment
                    {
                        public string Name { get; set; }
                        public string Description { get; set; }
                        public string Href { get; set; }
                        public FacebookPostType Fb_Object_Type { get; set; }
                        public string Fb_Object_Id { get; set; }
                
                        [JsonConverter(typeof(FacebookMediaJsonConverter))]
                        public List<FacebookMedia> { get; set; }
                
                        public string Permalink { get; set; }
                    }
                

                如果不使用 FacebookMediaJsonConverter,我会收到错误消息:无法将 JSON 对象反序列化为类型System.Collections.Generic.List`1[FacebookMedia]".这是有道理的,因为在 JSON 中,Media 不是一个集合.

                Without using the FacebookMediaJsonConverter, I get an error: Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[FacebookMedia]'. which makes sense, since in the JSON, Media is not a collection.

                我发现这篇文章描述了一个类似的问题,所以我试图走这条路:反序列化JSON,有时value是一个数组,有时是"(空字符串)

                I found this post which describes a similar problem, so I've attempted to go down this route: Deserialize JSON, sometimes value is an array, sometimes "" (blank string)

                我的转换器看起来像:

                public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                {
                     if (reader.TokenType == JsonToken.StartArray)
                          return serializer.Deserialize<List<FacebookMedia>>(reader);
                     else
                          return null;
                }
                

                效果很好,只是我现在遇到了一个新异常:

                Which works fine, except I now get a new exception:

                在 JsonSerializerInternalReader.cs 内部,CreateValueInternal():反序列化对象时出现意外令牌:PropertyName

                Inside JsonSerializerInternalReader.cs, CreateValueInternal(): Unexpected token while deserializing object: PropertyName

                reader.Value 的值是永久链接".我可以在 switch 中清楚地看到 JsonToken.PropertyName 没有案例.

                The value of reader.Value is "permalink". I can clearly see in the switch that there's no case for JsonToken.PropertyName.

                我需要在转换器中做一些不同的事情吗?谢谢你的帮助.

                Is there something I need to do differently in my converter? Thanks for any help.

                推荐答案

                JSON.NET 的开发人员最终帮助了项目 codeplex 网站.这是解决方案:

                The developer of JSON.NET ended up helping on the projects codeplex site. Here is the solution:

                问题是,当它是一个 JSON 对象时,我没有读取过去的属性.这是正确的代码:

                The problem was, when it was a JSON object, I wasn't reading past the attribute. Here is the correct code:

                public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                {
                    if (reader.TokenType == JsonToken.StartArray)
                    {
                        return serializer.Deserialize<List<FacebookMedia>>(reader);
                    }
                    else
                    {
                        FacebookMedia media = serializer.Deserialize<FacebookMedia>(reader);
                        return new List<FacebookMedia>(new[] {media});
                    }
                }
                

                James 也很友好地为上述方法提供了单元测试.

                James was also kind enough to provide unit tests for the above method.

                这篇关于有时是数组有时是对象时反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用 Json.net 反序列化 JSON 对象数组 下一篇:Json.Net 中的私人二传手

                相关文章

                最新文章

                  <bdo id='CefZc'></bdo><ul id='CefZc'></ul>

              • <small id='CefZc'></small><noframes id='CefZc'>

                  1. <tfoot id='CefZc'></tfoot>
                    <i id='CefZc'><tr id='CefZc'><dt id='CefZc'><q id='CefZc'><span id='CefZc'><b id='CefZc'><form id='CefZc'><ins id='CefZc'></ins><ul id='CefZc'></ul><sub id='CefZc'></sub></form><legend id='CefZc'></legend><bdo id='CefZc'><pre id='CefZc'><center id='CefZc'></center></pre></bdo></b><th id='CefZc'></th></span></q></dt></tr></i><div id='CefZc'><tfoot id='CefZc'></tfoot><dl id='CefZc'><fieldset id='CefZc'></fieldset></dl></div>
                  2. <legend id='CefZc'><style id='CefZc'><dir id='CefZc'><q id='CefZc'></q></dir></style></legend>