score:1
Accepted answer
You don't show how you initialize fakeGuidForExecutiveSummary
. Assuming you do it in the following way:
string fakeResponseFromExecutiveSummaryProxy = "{ \"DocumentID\": \"fakeGuid1\",\"documentNotes\": \"TestNotes1\"}";
var fakeResponse = JToken.Parse(fakeResponseFromExecutiveSummaryProxy);
var fakeGuidForExecutiveSummary = fakeResponse["DocumentID"];
Then the problem is that fakeGuidForExecutiveSummary
is a JValue
, not a JToken
or JArray
. Your code will throw the exception you see if you try to access a (nonexistent) child value by index.
Instead you need to do the following:
string response = @"[{ ""DocumentID"": ""fakeGuid1"",""documentNotes"": ""TestNotes1""}]";
JArray jsonResponse = JArray.Parse(response);
JToken token = jsonResponse[0];
//Value of token from Debugger - { "DocumentID": fakeGuid1","documentNotes": "TestNotes1"}
Assert.AreEqual(fakeGuidForExecutiveSummary, token["DocumentID"])
Update
Given your updated code, the problem is that your sample JSON response
has too many levels of string escaping: \\\"DocumentID\\\"
. You probably copied escaped strings shown in Visual Studio into the source code, then escaped them some again.
Change it to
string response = "[\r\n { \"DocumentID\": \"fakeGuid1\",\"documentNotes\": \"TestNotes1\"}\r\n]";
Source: stackoverflow.com
Related Query
- Getting Exception while accessing value of JToken - Cannot access child value on Newtonsoft.Json.Linq.JValue
- Cannot access child value on Newtonsoft.Json.Linq.JProperty -Error happens while checking JObject using LinQ
- C# Cannot access child value on Newtonsoft.Json.Linq.JProperty
- json Cannot access child value on Newtonsoft.Json.Linq.JValue
- Cannot access child value on Newtonsoft.Json.Linq.JValue during Neo4jConnection
- Cannot access child value on JValue in Azure Mobile App
- Check property exists to stop error; cannot access child value on Newtonsoft.Json.Linq.JProperty
- Getting Value cannot be null. parameter name: source on release but not in debug
- Value cannot be null. Parameter name: source
- How to resolve Value cannot be null. Parameter name: source in linq?
- Linq to Xml : Exception -The ' ' character, hexadecimal value 0x20, cannot be included in a name
- "Cannot access child value on Newtonsoft.Json.Linq.JValue."
- The given value of type String from the data source cannot be converted to type int of the specified target column
- Getting the error "cannot add an entity that already exists." while inserting a table's value in to DB without procedure
- "Value cannot be null" while creating an XElement from the attributes where one or more value is null
- XML to Linq getting child nodes with attribute value
- How to merge two lists while adding metadata indicating value source with LINQ statement?
- Accessing child value of JSON in C#
- Getting Child nodes of an xDocument element while itereating through the Xdocument
- Value cannot be null, r nparameter name source
- Name cannot begin with the '1' character, hexadecimal value 0x31. while reading from an xml file
- Getting null reference Exception while parsing XML through LINQ
- How to remove a node while preserving its value and child nodes?
- Getting an Exception "System.ArgumentException was unhandled by user code" while converting ObservableCollectionView into AsQueryable ()
- Cannot insert the value NULL into column ... Exception issues
- c# System.ArgumentNullException: Value cannot be null. Parameter name: source
- Only getting the first value while parsing an XML document using Linq
- LINQ - null exception on getting certain cell value
- Why am I getting a null exception when trying to assign a value to a nested object in C# asp.net 5?
- Get objects while getting couple of fields from latest child
More Query from same tag
- linq: groupby with multiple nullable types
- Remove duplicates from list based on multiple fields or columns
- Is the order of execution of Linq the reason for this catch?
- Linq : Select Different Object Left Join
- How to pass parameter to custom function from Linq method?
- Linq query can't be converted to SQL: how to optimize the code?
- Linq many to many relationship with no foreign key
- How to get multiple sums that are subqueries
- Using a Substring in LINQ causes query to time out?
- EF Core, querying data with one-to-one relationship based on an enum
- remove list-items with Linq when List.property = myValue
- How to build a LINQ expression with Contains method from IEnumerable?
- Converting Collection of Strings to Dictionary
- How can I union with another table linq
- Designing Fluent interface methods
- Converting an IQueryable<int?> to IQueryable<int> after nulls have been removed
- C# Expression class method extension, making string comparison case insensitive
- Get records via foreign key in another table in Entity Framework 6
- How to process dynamic tablenames
- How to write a Custom DynamicObject class that supports object initializers
- Access property in lambda expression from string when using LINQ
- Why do I need to initialize an out variable when using Linq
- LINQ Join on multiple columns with different record using extension method syntax
- How to handle no results in LINQ?
- Linq to Xml and custom xml entities
- Dataset to XMLDocument repeats three times
- Why does LINQ treat two methods that do the "same" thing differently?
- Using Rx (Reactive Extensions) to watch for specific item in ObservableCollection
- Another way of solving dictionary search (without LINQ)
- Using LINQ to return average of distinct values