zoukankan      html  css  js  c++  java
  • JSON的部分序列化

    http://hi.baidu.com/%B1%F9%D1%A9%D2%F8%C1%AB/blog/item/39a1c4fb0ae8ebccb58f31e0.html#0

    通常当用到大的Json文档的时候,你可能只对其中的一小个片段信息感兴趣。这种情况下你想把Json.Net转换为.Net 对象就会让人很困扰,因为你必须为Json的整个结果定义一个.Net的类。

    使用Json.Net很容易避开这个问题。在把它们传递到Json.Net序列化器之前,你可以使用Linq to Json 提取Json中你想要序列化的一些片段。

     string googleSearchText = @"{
                      ""responseData"": {
                        ""results"": [
                          {
                            ""GsearchResultClass"": ""GwebSearch"",
                            ""unescapedUrl"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",
                            ""url"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",
                            ""visibleUrl"": ""en.wikipedia.org"",
                            ""cacheUrl"": ""http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org"",
                            ""title"": ""<b>Paris Hilton</b> - Wikipedia, the free encyclopedia"",
                            ""titleNoFormatting"": ""Paris Hilton - Wikipedia, the free encyclopedia"",
                            ""content"": ""[1] In 2006, she released her debut album...""
                          },
                          {
                            ""GsearchResultClass"": ""GwebSearch"",
                            ""unescapedUrl"": ""http://www.imdb.com/name/nm0385296/"",
                            ""url"": ""http://www.imdb.com/name/nm0385296/"",
                            ""visibleUrl"": ""www.imdb.com"",
                            ""cacheUrl"": ""http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com"",
                            ""title"": ""<b>Paris Hilton</b>"",
                            ""titleNoFormatting"": ""Paris Hilton"",
                            ""content"": ""Self: Zoolander. Socialite <b>Paris Hilton</b>...""
                          }
                        ],
                        ""cursor"": {
                          ""pages"": [
                            {
                              ""start"": ""0"",
                              ""label"": 1
                            },
                            {
                              ""start"": ""4"",
                              ""label"": 2
                            },
                            {
                              ""start"": ""8"",
                              ""label"": 3
                            },
                            {
                              ""start"": ""12"",
                              ""label"": 4
                            }
                          ],
                          ""estimatedResultCount"": ""59600000"",
                          ""currentPageIndex"": 0,
                          ""moreResultsUrl"": ""http://www.google.com/search?oe=utf8&ie=utf8...""
                        }
                      },
                      ""responseDetails"": null,
                      ""responseStatus"": 200
                    }";
                JObject googleSearch = JObject.Parse(googleSearchText);
                // get JSON result objects into a list
                IList<JToken> results = googleSearch["responseData"]["results"].Children().ToList();

                // serialize JSON results into .NET objects
                IList<SearchResult> searchResults = new List<SearchResult>();
                foreach (JToken result in results)
                {
                    SearchResult searchResult = JsonConvert.DeserializeObject<SearchResult>(result.ToString());
                    searchResults.Add(searchResult);
                }

                // Title = <b>Paris Hilton</b> - Wikipedia, the free encyclopedia
                // Content = [1] In 2006, she released her debut album...
                // Url = http://en.wikipedia.org/wiki/Paris_Hilton

                // Title = <b>Paris Hilton</b>
                // Content = Self: Zoolander. Socialite <b>Paris Hilton</b>...
                // Url = http://www.imdb.com/name/nm0385296/

  • 相关阅读:
    windows系统下的文件夹链接功能mklink/linkd
    packetfence 7.2网络准入部署(一)
    packetfence 7.2网络准入部署(二)
    博客园转载其他博客园的文章:图片和源码
    使用ARP欺骗, 截取局域网中任意一台机器的网页请求,破解用户名密码等信息
    Huawei AP3030DN固件升级
    iOS高版本备份恢复到低版本系统的方法
    比较正确的 iPhone7/7+ 的进入DFU的方法是这样的
    使用kbmmw smarthttpservice 简单返回数据库结果
    kbmmw 5.07 正式发布
  • 原文地址:https://www.cnblogs.com/MyFlora/p/2451254.html
Copyright © 2011-2022 走看看