• 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/

  • 相关阅读:
    redis客户端windows版中文乱码解决方案
    nginx做负载均衡,怎么在有宕机情况出现时保证网站的响应速度
    支付宝同步和异步验签结果不一致的解决方法
    @ResponseBody中文乱码解决方案
    [javamail]AUTH LOGIN failed;Invalid username or password报错
    Could not load driverClass ${driverClassName} 的解决方案
    eclipse中,maven报错maven.multiModuleProjectDirectory system property is not set
    spring bean初始化和销毁方法
    关于静态资源是否应该放到WEB-INF目录
    使用Jedis出现Connection refused的解决方案
  • 原文地址:https://www.cnblogs.com/q28633999/p/2078412.html
Copyright © 2020-2023  润新知