![](/img/trans.png)
[英]C# JSON Parse gives error 'System.ArgumentNullException' when parsing JSON array
[英]System.ArgumentNullException when parsing Json in controller
我是C#和DotNet的新手,所以如果这是一个非常基本的问题,我深表歉意。我通过邮递员向我的应用程序发送了一些Json:
[
{
"id": 5,
"name": "Book",
"price": 12.49,
"import": false,
"exempt": true
}]
我返回此错误:
System.ArgumentNullException: Value cannot be null.
Parameter name: s
 at System.IO.StringReader..ctor(String s)
 at Newtonsoft.Json.Linq.JArray.Parse(String json, JsonLoadSettings settings)
 at StoreFront.Controllers.StoreFrontController.Post(String product) in C:\Users\adamg\source\repos\StoreFront\StoreFront\Controllers\StoreFrontController.cs:line 43
 at lambda_method(Closure , Object , Object[] )
 at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
 at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
 at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
 at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResourceFilter>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
 at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
 at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()
这是我的控制器Post方法:
[HttpPost]
public IActionResult Post([FromBody]string product)
{
JArray order = JArray.Parse(product);
IList<Order> Order = order.Select(p => new Order
{
Total = (Double)p["Price"],
}).ToList();
_context.Order.Add(Order[0]);
return Ok(order);
}
谢谢您的帮助!
至少在.NET Framework 4.5.2中,可以进行以下操作:
var expectedResults = JsonConvert.DeserializeObject<List<OrderDto>>(product);
假设您有以下课程:
public class OrderDto
{
public int id { get; set; }
public string name { get; set; }
public double price { get; set; }
public bool import { get; set; }
public bool exempt { get; set; }
}
希望这也适用于您。
JSON区分大小写。
Total = (Double)p["Price"], // does not exist
Total = (Double)p["price"], // ok
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.