0

I have the following JSON being sent from Postman to my Web API.

[
    {
        "salesId": "SO002494"
    },
    {
        "salesId": "SO002496"
    } 
]

Sales Model

public class Sales
{
    public string[] salesId { get; set; }
}
[HttpGet("postRouteOrder")]
public async Task<ActionResult<string>>PostRouteOrder([FromBody] Object json)
{
    Sales sales = new Sales();

    sales.salesId = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json.ToString());
           
    //string[] data = { "SO002604", "SO002606" };               
}

I want sales.salesId to be like data.

3
  • 1
    stackoverflow.com/a/9586683/13405106 Commented Apr 8, 2022 at 10:59
  • 1
    And it's weird that you post an object via [HttpGet]. You should use [HttpPost]. Commented Apr 8, 2022 at 11:07
  • This is because my web API is an interface between an application and a SOAP API server. The SOAP API will do the [HttpPost] Commented Apr 8, 2022 at 11:22

1 Answer 1

1

Would suggest changing the request body parameter type to List<SaleInput>.

using System.Linq;

[HttpPost]
public async Task<ActionResult<string>> PostRouteOrder ([FromBody] List<SaleInput> saleInputs)
{

    string[] data = saleInputs.Select(x => x.SalesId).ToArray();
}
public class SaleInput
{
    public string SalesId { get; set; }
}

Apart from that, instead of deserializing as dynamic, you should deserialize as List of objects and take the SalesId as an array.

List<SaleInput> saleInput = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SaleInput>>(/* JSON */);

sales.salesId = saleInputs.Select(x => x.SalesId)
        .ToArray();
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.