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 Articles
- 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
- Easy way to Populate a Dictionary<string,List<string>>
- Linq query not giving desired results
- Speeding up SQL to Linq ToList
- Why does Select turn my while loop into an infinite loop?
- How to keep showing LINQ results when 'Next' button pressed?
- LINQ join issue, returns all null value
- Compile time Type declaration without using dynamic in C#
- Linq query returning exception
- Convert String to linq condition Entity Framework core
- Linq query for two tables using SUM and Group By
- LINQ Select Many statement
- Linq check if null and send to razor view
- Is it possible to get other collection than IEnumerable<T> when using LINQ: XML to object?
- Linq MaxBy with all elements?
- LINQ very SLOW when making .toList()
- Is there some way to simplify access to the first member of a group in a LINQ GroupBy query?
- How to get Sub objects based on value using linq?
- LINQ2SQL - How to detect mutiple instances of one string within another
- Efficiently identifying if any item in first set matches any item in second set
- Linq to Entity having more than one where conditions