我需要一种非常非常快速的检查字符串是否为JSON的方法.我觉得这不是最好的方法:
function isJson($string) {
return ((is_string($string) &&
(is_object(json_decode($string)) ||
is_array(json_decode($string))))) ? true : false;
}
Run Code Online (Sandbox Code Playgroud)
任何表演爱好者都想改进这种方法吗?
在我的一个控制器动作中,我返回一个非常大JsonResult的填充网格.
我收到以下InvalidOperationException异常:
使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过maxJsonLength属性上设置的值.
不幸的是,将maxJsonLength属性设置web.config为更高的值不会产生任何影响.
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
Run Code Online (Sandbox Code Playgroud)
我不想把它作为一个字符串传回去,就像这个 SO答案中提到的那样.
在我的研究中,我遇到了这篇博文,其中建议编写自己的ActionResult(例如LargeJsonResult : JsonResult)来绕过这种行为.
这是唯一的解决方案吗?
这是ASP.NET MVC中的错误吗?
我错过了什么吗?
非常感激任何的帮助.
是否可以在ASP.NET MVC 3中使用JSON.NET作为默认的JSON序列化程序?
根据我的研究,似乎完成此任务的唯一方法是扩展ActionResult,因为MVC3中的JsonResult不是虚拟的 ......
我希望在ASP.NET MVC 3中有一种方法可以指定一个可插入的提供程序来序列化为JSON.
思考?
我有一些存储在DB中的存储JSON字符串,我想以JsonResult的形式返回给客户端.我知道Json(对象)将一个对象变成JsonResult但是如果我已经将结果放在一个字符串中呢?我可以把它投射到JsonResult
如果我有这样的控制器:
[HttpPost]
public JsonResult FindStuff(string query)
{
var results = _repo.GetStuff(query);
var jsonResult = results.Select(x => new
{
id = x.Id,
name = x.Foo,
type = x.Bar
}).ToList();
return Json(jsonResult);
}
Run Code Online (Sandbox Code Playgroud)
基本上,我从我的存储库中获取东西,然后将其投影到一个List<T>匿名类型中.
我该如何对其进行单元测试?
System.Web.Mvc.JsonResult有一个叫做的属性Data,但它的类型object,正如我们所期望的那样.
那么这是否意味着如果我想测试JSON对象具有我期望的属性("id","name","type"),我必须使用反射?
编辑:
这是我的测试:
// Arrange.
const string autoCompleteQuery = "soho";
// Act.
var actionResult = _controller.FindLocations(autoCompleteQuery);
// Assert.
Assert.IsNotNull(actionResult, "No ActionResult returned from action method.");
dynamic jsonCollection = actionResult.Data;
foreach (dynamic json in jsonCollection)
{
Assert.IsNotNull(json.id,
"JSON record does …Run Code Online (Sandbox Code Playgroud) 我有两个问题:
JSONResult和ActionResult有什么区别?
何时在MVC中使用JSONResult?
asp.net-mvc jsonresult asp.net-mvc-3 asp.net-mvc-4 asp.net-mvc-5
我基于他UserId作为JsonResult 获取用户的记录...
public JsonResult GetClients(int currentPage, int pageSize)
{
if (Session["UserId"] != "")
{
var clients = clirep.FindAllClients().AsQueryable();
var count = clients.Count();
var results = new PagedList<ClientBO>(clients, currentPage - 1, pageSize);
var genericResult = new { Count = count, Results = results };
return Json(genericResult);
}
else
{
//return RedirectToAction("Index","Home");
}
}
Run Code Online (Sandbox Code Playgroud)
如何从asp.net mvc中的JsonResult方法重定向到控制器操作?任何建议......
编辑: 这似乎不起作用......
if (Session["UserId"] != "")
{
var clients = clirep.FindAllClients().AsQueryable();
var count = clients.Count();
var results = new PagedList<ClientBO>(clients, currentPage - 1, pageSize); …Run Code Online (Sandbox Code Playgroud) 在ASP.NET Core 2.0中工作的控制器:
[Produces("application/json")]
[Route("api/[controller]")]
[ApiController]
public class GraficResourcesApiController : ControllerBase
{
private readonly ApplicationDbContext _context;
public GraficResourcesApiController(ApplicationDbContext context)
{
_context = context;
}
[HttpGet]
public JsonResult GetGrafic(int ResourceId)
{
var sheduling = new List<Sheduling>();
var events = from e in _context.Grafic.Where(c=>c.ResourceId == ResourceId)
select new
{
id = e.Id,
title = e.Personals.Name,
start = e.DateStart,
end = e.DateStop,
color = e.Personals.Color,
personalId = e.PersonalId,
description = e.ClientName
};
var rows = events.ToArray();
return Json(rows);
}
}
Run Code Online (Sandbox Code Playgroud)
在ASP.NET Core 2.1中 …
我正在尝试使用ViewData将Json传递给我的View
调节器
ViewData("JsonRegionList") = Json(RegionService.GetActiveRegions())
Run Code Online (Sandbox Code Playgroud)
视图
$("input#UserRegion").autocomplete({
source:"<%: ViewData("JsonRegionList").ToString %>",
minLength: 3,
Run Code Online (Sandbox Code Playgroud)
但我遇到的问题是输出源看起来像
$("input#UserRegion").autocomplete({
source:"System.Web.Mvc.JsonResult",
minLength: 3,
Run Code Online (Sandbox Code Playgroud)
这显然是不对的.我错过了什么基本的东西?
我有一个返回的方法
return new System.Web.Mvc.JsonResult()
{
Data = new
{
Status = "OK",
}
}
Run Code Online (Sandbox Code Playgroud)
我需要编写一个单元测试,我需要验证它jsonResult.Data.status= "OK".
我如何阅读状态属性?
更新:我尝试了[assembly:InternalsVisibleTo("TestingAssemblyName")],但这没有帮助.我一直收到错误{"'System.Web.Mvc.JsonResult'不包含'Status'的定义"}
此外,我想我不想修改我正在测试的代码.
所以我接受了Jon的建议并使用了反思.
var type = jsonResult.Data.GetType();
var pinfo = type.GetProperty("Status");
string statusValue = pinfo.GetValue(jsonResult.Data,null).ToString();
Assert.AreEqual("OK", statusValue);
Run Code Online (Sandbox Code Playgroud) jsonresult ×10
asp.net-mvc ×5
json ×5
c# ×3
asp.net ×1
exception ×1
json.net ×1
php ×1
unit-testing ×1
viewdata ×1