Json.Net學習筆記(十四) JSON的部分序列化

來源:互聯網
上載者:User

通常當用到大的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"": ""http://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/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.