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.

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.

Fragments Object Copy imageCopy
1public class SearchResult
2{
3    public string Title { get; set; }
4    public string Content { get; set; }
5    public string Url { get; set; }
6}
Serializing Partial JSON Fragment Example Copy imageCopy
 1      string googleSearchText = @"{
 2  'responseData': {
 3    'results': [
 4      {
 5        'GsearchResultClass': 'GwebSearch',
 6        'unescapedUrl': 'http://en.wikipedia.org/wiki/Paris_Hilton',
 7        'url': 'http://en.wikipedia.org/wiki/Paris_Hilton',
 8        'visibleUrl': 'en.wikipedia.org',
 9        'cacheUrl': 'http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org',
10        'title': '<b>Paris Hilton</b> - Wikipedia, the free encyclopedia',
11        'titleNoFormatting': 'Paris Hilton - Wikipedia, the free encyclopedia',
12        'content': '[1] In 2006, she released her debut album...'
13      },
14      {
15        'GsearchResultClass': 'GwebSearch',
16        'unescapedUrl': 'http://www.imdb.com/name/nm0385296/',
17        'url': 'http://www.imdb.com/name/nm0385296/',
18        'visibleUrl': 'www.imdb.com',
19        'cacheUrl': 'http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com',
20        'title': '<b>Paris Hilton</b>',
21        'titleNoFormatting': 'Paris Hilton',
22        'content': 'Self: Zoolander. Socialite <b>Paris Hilton</b>...'
23      }
24    ],
25    'cursor': {
26      'pages': [
27        {
28          'start': '0',
29          'label': 1
30        },
31        {
32          'start': '4',
33          'label': 2
34        },
35        {
36          'start': '8',
37          'label': 3
38        },
39        {
40          'start': '12',
41          'label': 4
42        }
43      ],
44      'estimatedResultCount': '59600000',
45      'currentPageIndex': 0,
46      'moreResultsUrl': 'http://www.google.com/search?oe=utf8&ie=utf8...'
47    }
48  },
49  'responseDetails': null,
50  'responseStatus': 200
51}";
52
53      JObject googleSearch = JObject.Parse(googleSearchText);
54
55      // get JSON result objects into a list
56      IList<JToken> results = googleSearch["responseData"]["results"].Children().ToList();
57
58      // serialize JSON results into .NET objects
59      IList<SearchResult> searchResults = new List<SearchResult>();
60      foreach (JToken result in results)
61      {
62          SearchResult searchResult = JsonConvert.DeserializeObject<SearchResult>(result.ToString());
63          searchResults.Add(searchResult);
64      }
65
66      // Title = <b>Paris Hilton</b> - Wikipedia, the free encyclopedia
67      // Content = [1] In 2006, she released her debut album...
68      // Url = http://en.wikipedia.org/wiki/Paris_Hilton
69
70      // Title = <b>Paris Hilton</b>
71      // Content = Self: Zoolander. Socialite <b>Paris Hilton</b>...
72      // Url = http://www.imdb.com/name/nm0385296/

See Also