久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <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 class="qwawimqqmiuu" id='S6SzJ'><tfoot id='S6SzJ'></tfoot><dl id='S6SzJ'><fieldset id='S6SzJ'></fieldset></dl></div>
      <tfoot id='S6SzJ'></tfoot>

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

        有時是數組有時是對象時反序列化JSON

        Deserializing JSON when sometimes array and sometimes object(有時是數組有時是對象時反序列化JSON)

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

            <tfoot id='i5gsB'></tfoot>
          • <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 class="qwawimqqmiuu" id='i5gsB'><tfoot id='i5gsB'></tfoot><dl id='i5gsB'><fieldset id='i5gsB'></fieldset></dl></div>

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

                  本文介紹了有時是數組有時是對象時反序列化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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                  XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                  Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                  Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                  delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                  Parse malformed XML(解析格式錯誤的 XML)
                    <bdo id='CefZc'></bdo><ul id='CefZc'></ul>

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

                        1. <tfoot id='CefZc'></tfoot>
                            <tbody id='CefZc'></tbody>
                          <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 class="qwawimqqmiuu" id='CefZc'><tfoot id='CefZc'></tfoot><dl id='CefZc'><fieldset id='CefZc'></fieldset></dl></div>
                          • <legend id='CefZc'><style id='CefZc'><dir id='CefZc'><q id='CefZc'></q></dir></style></legend>

                            主站蜘蛛池模板: 国产高清在线视频 | 91精品国产乱码久久久久久 | 精品国产乱码一区二区三区a | 免费看的黄网站 | 国产乱码精品1区2区3区 | 免费同性女女aaa免费网站 | 日本一区二区不卡 | 麻豆av在线免费观看 | 国产激情在线播放 | 久久久久中文字幕 | www.国产.com | 成人久久网 | 毛片免费在线观看 | 无吗视频| 国产成人午夜精品影院游乐网 | 毛片国产 | 国产成人精品一区二区三区四区 | 99精品在线 | 亚洲国产精品第一区二区 | 成年人在线观看视频 | 一级全黄少妇性色生活免费看 | 亚洲精品第一页 | 午夜视频网 | 91精品国产91久久久久久吃药 | 日韩欧美中文字幕在线观看 | 亚洲风情在线观看 | 四虎在线视频 | 亚洲免费大片 | 四虎最新 | 精品国产一区二区三区观看不卡 | 亚洲综合在线一区二区 | 亚洲成人精选 | 久久久久久久国产 | 成人在线国产 | 在线观看成人小视频 | 中文字幕视频三区 | 久久不卡 | 国产一区二区精品在线观看 | 91视频电影 | 二区不卡 | 日本精品久久久久 |