从JsonResult获取数据 [英] Get data from JsonResult

查看:94
本文介绍了从JsonResult获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码在ASP.NET Core中.MVC控制器返回Json(someData).我正在尝试在单元测试中检索数据.我能做的最好的就是

The code is in ASP.NET Core. MVC controller returns Json(someData). I am trying to retrieve the data in the Unit test. The best I can do is to have

string data = JsonConvert.SerializeObject(jsonResult.Value);

,然后比较字符串.但是我更希望获得一个对象或一个数组,以便能够进行一些更具体的比较.有趣的是,在调试器下,我可以看到 jsonResult.Value 的类型为 Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable< MyType> ,甚至还有结果视图警告不要扩大它;否则如果我扩展它,我将得到我想要的!在我的情况下,它是一个由4个元素组成的 MyType 对象数组.但是,如果我在jsonResult.Value中从i执行类似的操作,则选择i 会收到错误

and then compare the string. But I would prefer to get an object or an array to be able to do some more specific comparisons. Interesting, under debugger I can see that jsonResult.Value is of type Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<MyType> and there is even Results View that warns not to expand it or else; and if I expand it, I get exactly what I want! In my case it is a 4-element array of MyType objects. However, if I do something like from i in jsonResult.Value select i I get an error

Could not find an implementation of the query pattern for source type 'object'. 
'Select' not found

我希望有一个比比较JSON字符串更好的方法!

I hope there is a better way than comparing a JSON string!

推荐答案

尝试使用Cast方法

(从jsonResult.Value.Cast< MyType>()中的i中选择i)

修改:更新的答案

 from i in ((IQueryable< MyType >)js.Value) select i

这篇关于从JsonResult获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆