a575963da9
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
116 lines
8.1 KiB
HTML
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;">"Manufacturers[0].Name"</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't be
|
|
found at the path'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;">@"{</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Stores"": [</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Lambton Quay"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Willis Street""</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ],</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Manufacturers"": [</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Acme Co"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Products"": [</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Anvil"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Price"": 50</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;"> },</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Contoso"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Products"": [</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Elbow Grease"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Price"": 99.95</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;"> ""Name"": ""Headlight Fluid"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Price"": 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;"> }</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;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: blue;">string</span> name = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">"Manufacturers[0].Name"</span>);</pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// Acme Co</span></pre>
|
|
<pre style="margin: 0px;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: blue;">decimal</span> productPrice = (<span style="color: blue;">decimal</span>)o.SelectToken(<span style="color: #a31515;">"Manufacturers[0].Products[0].Price"</span>);</pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// 50</span></pre>
|
|
<pre style="margin: 0px;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: blue;">string</span> productName = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">"Manufacturers[1].Products[0].Name"</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><<span style="color: blue;">string</span>> storeNames = o.SelectToken(<span style="color: #a31515;">"Stores"</span>).Select(s => (<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;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span><<span style="color: blue;">string</span>> firstProductNames = o[<span style="color: #a31515;">"Manufacturers"</span>].Select(m => (<span style="color: blue;">string</span>)m.SelectToken(<span style="color: #a31515;">"Products[1].Name"</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;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: blue;">decimal</span> totalPrice = o[<span style="color: #a31515;">"Manufacturers"</span>].Sum(m => (<span style="color: blue;">decimal</span>)m.SelectToken(<span style="color: #a31515;">"Products[0].Price"</span>));</pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// 149.95</span></pre>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div id="footer"></div>
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html> |