Reputation: 1
I'm new to C# and DotNet so I apologize if this is a pretty basic question.I am sending some Json through Postman to my application:
[
{
"id": 5,
"name": "Book",
"price": 12.49,
"import": false,
"exempt": true
}]
I am returned this error:
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()
Here is my controller Post method:
[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);
}
Thanks for the help!
Upvotes: 0
Views: 591
Reputation:
JSON is case-sensitive.
Total = (Double)p["Price"], // does not exist
Total = (Double)p["price"], // ok
Upvotes: 0
Reputation: 20058
In .NET Framework 4.5.2 at least, following would work:
var expectedResults = JsonConvert.DeserializeObject<List<OrderDto>>(product);
assuming you had a class of:
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; }
}
hope that this applies to you as well.
Upvotes: 1