Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

116 lines
8.1 KiB
HTML

<html>
<head>
<title>Querying LINQ to JSON with SelectToken</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">Querying LINQ to JSON with SelectToken</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p><a href="./html/M_Newtonsoft_Json_Linq_JToken_SelectToken.htm">SelectToken</a> provides a method to query LINQ to JSON using a single string path to a
desired <a href="./html/T_Newtonsoft_Json_Linq_JToken.htm">JToken</a>. SelectToken makes dynamic queries
easy because the entire
query is defined in a string.</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;">string</span> name = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">&quot;Manufacturers[0].Name&quot;</span>);</pre>
</div>
</div>
</div>
<h3>SelectToken</h3>
<p>SelectToken is a method on JToken and takes a string path to a child token.
SelectToken returns the child token or a null reference if a token couldn&#39;t be
found at the path&#39;s location.</p>
<p>The
path is made up of property names and array indexes separated by periods. Array indexes
can use either square or round brackets. Both
of the following are valid paths and are equivalent to each other: <code>Manufacturers[0].Name</code>
and <code>Manufacturers(0).Name</code>.</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: #2b91af;">JObject</span> o = <span style="color: #2b91af;">JObject</span>.Parse(<span style="color: #a31515;">@&quot;{</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;Stores&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;Lambton Quay&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;Willis Street&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ],</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;Manufacturers&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Acme Co&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;Products&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Anvil&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Price&quot;&quot;: 50</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Contoso&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;Products&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Elbow Grease&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Price&quot;&quot;: 99.95</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Headlight Fluid&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Price&quot;&quot;: 4</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">}&quot;</span>);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> name = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">&quot;Manufacturers[0].Name&quot;</span>);</pre>
<pre style="margin: 0px;"><span style="color: green;">// Acme Co</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">decimal</span> productPrice = (<span style="color: blue;">decimal</span>)o.SelectToken(<span style="color: #a31515;">&quot;Manufacturers[0].Products[0].Price&quot;</span>);</pre>
<pre style="margin: 0px;"><span style="color: green;">// 50</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> productName = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">&quot;Manufacturers[1].Products[0].Name&quot;</span>);</pre>
<pre style="margin: 0px;"><span style="color: green;">// Elbow Grease</span></pre>
</div>
</div>
</div>
<h3>SelectToken with LINQ</h3>
<p>SelectToken can be used in combination with standard LINQ methods.</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: #2b91af;">IList</span>&lt;<span style="color: blue;">string</span>&gt; storeNames = o.SelectToken(<span style="color: #a31515;">&quot;Stores&quot;</span>).Select(s =&gt; (<span style="color: blue;">string</span>)s).ToList();</pre>
<pre style="margin: 0px;"><span style="color: green;">// Lambton Quay</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Willis Street</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span>&lt;<span style="color: blue;">string</span>&gt; firstProductNames = o[<span style="color: #a31515;">&quot;Manufacturers&quot;</span>].Select(m =&gt; (<span style="color: blue;">string</span>)m.SelectToken(<span style="color: #a31515;">&quot;Products[1].Name&quot;</span>)).ToList();</pre>
<pre style="margin: 0px;"><span style="color: green;">// null</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Headlight Fluid</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">decimal</span> totalPrice = o[<span style="color: #a31515;">&quot;Manufacturers&quot;</span>].Sum(m =&gt; (<span style="color: blue;">decimal</span>)m.SelectToken(<span style="color: #a31515;">&quot;Products[0].Price&quot;</span>));</pre>
<pre style="margin: 0px;"><span style="color: green;">// 149.95</span></pre>
</div>
</div>
</div>
<div id="footer"></div>
</div>
</body>
</html>