1

I have passed Json object though Postman. And I have no idea how can I loop this with .NetCore 1.1 Web Service.

Json Object passed by Postman:

{
 [
  {"id":"1","name":"Peter"}
  ,
  {"id":"2","name":"Mary"}
 ]
}

.net core 1.1 coding:

[HttpPut]
[Route("all")]
public IActionResult UpdateName([FromBody] dynamic JSON){
  var RootObjects = JsonConvert.DeserializeObject<List<RootObject>>(JSON);
  foreach (var x in RootObjects){
   // do something here...
  }
}

RootObjects:

private class RootObject
{
    public string id { get; set; }
    public string name { get; set; }
}

And I get error below:

System.ArgumentNullException: Value cannot be null.\r\nParameter name: value\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)\r\n at CallSite.Target(Closure , CallSite , Type , Object )\r\n

0

1 Answer 1

1

Maybe your json is invalid.Change your json string to:

 [
  {"id":"1","name":"Peter"},
  {"id":"2","name":"Mary"}
 ]

And change your controller action to:

   [HttpPut]
    public IActionResult Test([FromBody] List<RootObject> list)
    {
        foreach (var item in list)
        {
            Console.WriteLine(item.name);
        }

        return Json("ok");
    }

And also the RootObject class need to Public:

     public class RootObject
     {
       public string id { get; set; }
       public string name { get; set; }
     }

Use the postman put string in Content-Type:application/json. And i saw the correct result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.