Add documentation and split JSON Path into its own documentation section (#1925)
This commit is contained in:
+104
@@ -0,0 +1,104 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
using System.Linq;
|
||||
#endif
|
||||
using System.Text;
|
||||
#if DNXCORE50
|
||||
using Xunit;
|
||||
using Test = Xunit.FactAttribute;
|
||||
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
|
||||
#else
|
||||
using NUnit.Framework;
|
||||
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.JsonPath
|
||||
{
|
||||
[TestFixture]
|
||||
public class QueryJsonSelectTokenWithLinq : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
JObject o = JObject.Parse(@"{
|
||||
'Stores': [
|
||||
'Lambton Quay',
|
||||
'Willis Street'
|
||||
],
|
||||
'Manufacturers': [
|
||||
{
|
||||
'Name': 'Acme Co',
|
||||
'Products': [
|
||||
{
|
||||
'Name': 'Anvil',
|
||||
'Price': 50
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
'Name': 'Contoso',
|
||||
'Products': [
|
||||
{
|
||||
'Name': 'Elbow Grease',
|
||||
'Price': 99.95
|
||||
},
|
||||
{
|
||||
'Name': 'Headlight Fluid',
|
||||
'Price': 4
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}");
|
||||
|
||||
string[] storeNames = o.SelectToken("Stores").Select(s => (string)s).ToArray();
|
||||
|
||||
Console.WriteLine(string.Join(", ", storeNames));
|
||||
// Lambton Quay, Willis Street
|
||||
|
||||
string[] firstProductNames = o["Manufacturers"].Select(m => (string)m.SelectToken("Products[1].Name"))
|
||||
.Where(n => n != null).ToArray();
|
||||
|
||||
Console.WriteLine(string.Join(", ", firstProductNames));
|
||||
// Headlight Fluid
|
||||
|
||||
decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price"));
|
||||
|
||||
Console.WriteLine(totalPrice);
|
||||
// 149.95
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(149.95m, totalPrice);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user