我正在測試用例來模擬我的C#方法。我無法使用令牌[「DocumentID」]訪問JToken的DocumentID屬性。我得到System.InvalidOperationException - 「無法訪問Newtonsoft.Json.Linq.JValue上的子值」。訪問JToken值時獲取異常 - 無法訪問Newtonsoft.Json.Linq.J上的子值
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? – dbc 2015-03-13 17:16:10
另外,什麼是'fakeGuidForExecutiveSummary'? – dbc 2015-03-13 17:23:13
請嘗試創建一個[最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve)代碼來演示您的問題。既然你省略了一些步驟(比如初始化'fakeGuidForExecutiveSummary'),我們只能猜測問題是什麼。 – dbc 2015-03-13 17:36:17