a575963da9
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
125 lines
12 KiB
HTML
125 lines
12 KiB
HTML
<html>
|
|
|
|
<head>
|
|
<title>Serializing Partial JSON Fragments</title>
|
|
<link href="styles.css" rel="stylesheet" type="text/css" />
|
|
<link href="custom.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="control">
|
|
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
|
<span class="topicTitle">Serializing Partial JSON Fragments</span></div>
|
|
|
|
<div id="content">
|
|
<span style="color: DarkGray"> </span>
|
|
<p>Often when working with large JSON documents you're only interested in a small fragment of information.
|
|
This scenario can be annoying when you want to serialize that Json.NET into .NET
|
|
objects because you have to define .NET classes for the entire JSON result.</p>
|
|
<p>With Json.NET it is easy to get around this problem. Using LINQ to JSON you can
|
|
extract the pieces of JSON you want to serialize before passing them to the Json.NET serializer.</p>
|
|
|
|
<div class="overflowpanel">
|
|
<div class="code">
|
|
|
|
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
|
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">SearchResult</span></pre>
|
|
<pre style="margin: 0px;">{</pre>
|
|
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Title { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
|
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Content { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
|
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Url { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
|
<pre style="margin: 0px;">}</pre>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div class="overflowpanel">
|
|
<div class="code">
|
|
|
|
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
|
<pre style="margin: 0px;"><span style="color: blue;">string</span> googleSearchText = <span style="color: #a31515;">@"{</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""responseData"": {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""results"": [</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""GsearchResultClass"": ""GwebSearch"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""unescapedUrl"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""visibleUrl"": ""en.wikipedia.org"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""cacheUrl"": ""http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""title"": ""<b>Paris Hilton</b> - Wikipedia, the free encyclopedia"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""titleNoFormatting"": ""Paris Hilton - Wikipedia, the free encyclopedia"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""content"": ""[1] In 2006, she released her debut album...""</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""GsearchResultClass"": ""GwebSearch"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""unescapedUrl"": ""http://www.imdb.com/name/nm0385296/"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://www.imdb.com/name/nm0385296/"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""visibleUrl"": ""www.imdb.com"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""cacheUrl"": ""http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""title"": ""<b>Paris Hilton</b>"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""titleNoFormatting"": ""Paris Hilton"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""content"": ""Self: Zoolander. Socialite <b>Paris Hilton</b>...""</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ],</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""cursor"": {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""pages"": [</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""0"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 1</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""4"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 2</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""8"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 3</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""12"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 4</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ],</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""estimatedResultCount"": ""59600000"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""currentPageIndex"": 0,</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""moreResultsUrl"": ""http://www.google.com/search?oe=utf8&ie=utf8...""</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""responseDetails"": null,</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""responseStatus"": 200</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>;</pre>
|
|
<pre style="margin: 0px;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: #2b91af;">JObject</span> googleSearch = <span style="color: #2b91af;">JObject</span>.Parse(googleSearchText);</pre>
|
|
<pre style="margin: 0px;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// get JSON result objects into a list</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span><<span style="color: #2b91af;">JToken</span>> results = googleSearch[<span style="color: #a31515;">"responseData"</span>][<span style="color: #a31515;">"results"</span>].Children().ToList();</pre>
|
|
<pre style="margin: 0px;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// serialize JSON results into .NET objects</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span><<span style="color: #2b91af;">SearchResult</span>> searchResults = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">SearchResult</span>>();</pre>
|
|
<pre style="margin: 0px;"><span style="color: blue;">foreach</span> (<span style="color: #2b91af;">JToken</span> result <span style="color: blue;">in</span> results)</pre>
|
|
<pre style="margin: 0px;">{</pre>
|
|
<pre style="margin: 0px;"> <span style="color: #2b91af;">SearchResult</span> searchResult = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">SearchResult</span>>(result.ToString());</pre>
|
|
<pre style="margin: 0px;"> searchResults.Add(searchResult);</pre>
|
|
<pre style="margin: 0px;">}</pre>
|
|
<pre style="margin: 0px;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// Title = <b>Paris Hilton</b> - Wikipedia, the free encyclopedia</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// Content = [1] In 2006, she released her debut album...</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// Url = http://en.wikipedia.org/wiki/Paris_Hilton</span></pre>
|
|
<pre style="margin: 0px;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// Title = <b>Paris Hilton</b></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// Content = Self: Zoolander. Socialite <b>Paris Hilton</b>...</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// Url = http://www.imdb.com/name/nm0385296/</span></pre>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div id="footer"></div>
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html> |