• Json.Net学习笔记(十四) JSON的部分序列化


    通常当用到大的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/

  • 相关阅读:
    [HDU] 1016 Prime Ring Problem(DFS)
    ACM、OI等比赛中的程序对拍问题
    [POJ] 1948 Triangular Pastures (DP)
    [POJ] 1606 Jugs(BFS+路径输出)
    [百度2015春季实习生招聘附加题] 今天要吃点好的!
    Idea 搭建Maven--web项目(MVC)
    Python自动化测试框架——数据驱动(从文件中读取)
    Python自动化测试框架——数据驱动(从代码中读取)
    selenium——操作滚动条
    Python自动化测试框架——生成测试报告
  • 原文地址:https://www.cnblogs.com/q28633999/p/2078412.html
Copyright © 2020-2023  润新知