Json.Net学习笔记(十四) JSON的部分序列化
时间:2011-06-11 来源:资源收集
通常当用到大的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/










