c# - JsonConvert.Deserialize 来自 MVC 操作的 JsonResult

标签 c# json.net

应该很简单!我怎样才能完成以下任务?

JsonResult result = JsonConvert.Deserialize(CheckPlan());

CheckPlan() 返回的地方:

return Json(new { success = success }, JsonRequestBehavior.AllowGet);

我无法解析 JsonResult 返回的成功 bool 值。我试着把 <Dictionary<string,string>>就在反序列化之后,但它在语法上犹豫不决。用作类型与变量等。

正确的做法是什么?

最佳答案

我知道这是一篇旧帖子,但我遇到了完全相同的问题,我按如下方式解决了这个问题:

无需使用解串器!

dynamic result = CheckPlan().Data;    
Console.WriteLine(result.success);

在我的案例中,我正在为 MVC Controller 方法编写单元测试。由于测试方法在他们自己的项目中,我必须让他们访问 MVC 项目的内部,以便 dynamic 可以访问结果的 Data 对象的属性。为此,将以下行添加到 MVC 项目的 AssemblyInfo.cs 中:

// Allow the test project access to internals of MyProject.Web
[assembly: InternalsVisibleTo("MyProject.Test")]

关于c# - JsonConvert.Deserialize 来自 MVC 操作的 JsonResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26533796/

相关文章:

c# - 转换泛型参数

C# 通过引用传递值类型数组的元素

c# - 如何使用 P/Invoke 在 C# 中返回列表?

c# - 使用 Newtonsoft JSON.Net 在反序列化之前合并源 JSON

c#-4.0 - JsonConvert.DeserializeObject<T>(JsonString) 将所有 Properties<T> 返回为 Null

c# - 如何从 Referrer Uri 中获取 Controller 和 Action 名称?

C# String to DateTime 这种格式 2018-05-26T00 :00:00

c# - Null 合并运算符为动态对象的属性返回 null

c# - 我可以使用 Camelcase convert 属性得到什么,比如 ID(两个大写字母)

C# JSON反序列化: can I intercept the deserialization and optionally change the result?