【发布时间】:2015-05-16 06:21:55
【问题描述】:
我正在开发一个测试用例来模拟我的 C# 方法。我无法使用 token["DocumentID"] 访问 JToken 的 DocumentID 属性。我收到 System.InvalidOperationException -“无法访问 Newtonsoft.Json.Linq.JValue 上的子值”。
string response = "[\r\n \"{ \\\"DocumentID\\\": \\\"fakeGuid1\\\",\\\"documentNotes\\\": \\\"TestNotes1\\\"}\"\r\n]";
//Response has escape charaters as this is being returned by a mockMethod which is supposed to return JSon.ToString().
string[] fakeGuidForExecutiveSummary = new string[]{"fakeGuid1"};
string fakeResponseFromExecutiveSummaryProxy = "{ \"DocumentID\": \"fakeGuid1\",\"documentNotes\": \"TestNotes1\"}";
JArray jsonResponse = JArray.Parse(response);
//Value of jsonResponse from Debugger - {[ "{ \"DocumentID\": "fakeGuid1\",\"documentNotes\": \"TestNotes1\"}" ]}
JToken token = jsonResponse[0];
//Value of token from Debugger - { "DocumentID": fakeGuid1","documentNotes": "TestNotes1"}
Assert.AreEqual(fakeGuidForExecutiveSummary[0], token["DocumentID"]);
【问题讨论】:
-
什么是
response?另外,您根本没有使用fakeResponseFromExecutiveSummaryProxy,那么您实际使用的是什么JSON? -
还有,
fakeGuidForExecutiveSummary是什么? -
请尝试创建一个Minimal, Complete, and Verifiable example 的代码来演示您的问题。由于您省略了一些步骤(例如初始化
fakeGuidForExecutiveSummary),我们只能猜测问题所在。 -
我刚刚编辑了我的问题。谢谢。
-
fakeGuidForExecutiveSummary 等于响应。