forked from JamesNK/Newtonsoft.Json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryingLINQtoJSON.aml
56 lines (46 loc) · 3.19 KB
/
QueryingLINQtoJSON.aml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?xml version="1.0" encoding="utf-8"?>
<topic id="QueryingLINQtoJSON" revisionNumber="1">
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
<introduction>
<para>LINQ to JSON provides a number of methods for getting data from its objects. The index methods on JObject/JArray let you quickly get data by its property name
on an object or index in a collection, while <codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Children</codeEntityReference> lets you get ranges
of data as <codeInline>IEnumerable<JToken></codeInline> to then query using LINQ.</para>
</introduction>
<section>
<title>Getting values by Property Name or Collection Index</title>
<content>
<!-- Uncomment this to create a sub-section outline
<autoOutline /> -->
<para>The simplest way to get a value from LINQ to JSON is to use the
<codeEntityReference>P:Newtonsoft.Json.Linq.JToken.Item(System.Object)</codeEntityReference> index on
JObject/JArray and then cast the returned <codeEntityReference>T:Newtonsoft.Json.Linq.JValue</codeEntityReference> to the type you want.
</para>
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonSimpleQuerying" title="Getting JSON Values" />
</content>
</section>
<section>
<title>Querying with LINQ</title>
<content>
<para>JObject/JArray can also be queried using LINQ. <codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Children</codeEntityReference>
returns the children values of a JObject/JArray
as an <codeInline>IEnumerable<JToken></codeInline> that can then be queried with the standard Where/OrderBy/Select LINQ operators.</para>
<alert class="note">
<para><codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Children</codeEntityReference> returns all the children of a token. If it is a
JObject it will return a collection of properties to work with, and if
it is a JArray you will get a collection of the array's values.</para>
</alert>
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonQuerying" title="Querying JSON" />
<para>LINQ to JSON can also be used to manually convert JSON to a .NET object.</para>
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonDeserializeObject" title="Deserializing Using LINQ Objects" />
<para>Manually serializing and deserializing between .NET objects is useful when you are
working with JSON that doesn't closely match your .NET objects.</para>
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonDeserializeExample" title="Deserializing Using LINQ Example" />
</content>
</section>
<relatedTopics>
<link xlink:href="LINQtoJSON" />
<codeEntityReference>P:Newtonsoft.Json.Linq.JToken.Item(System.Object)</codeEntityReference>
<codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Children</codeEntityReference>
</relatedTopics>
</developerConceptualDocument>
</topic>