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

  • 相关阅读:
    html知识点
    BFC的布局规则以及触发条件
    父元素与子元素之间的margin-top问题(css hack)
    加overflow-hidden就可以解决高度塌陷问题,overflow-触发BFC
    子元素margin-top为何会影响父元素?
    子div设置margin-top使得父div也跟着向下移动
    vue 之 mongodb安装问题
    vue 之 nginx原理(webpack环境下配置)
    vue 之 PC端项目配置
    ASP.NET CORE (一)
  • 原文地址:https://www.cnblogs.com/MyFlora/p/2451254.html
Copyright © 2020-2023  润新知