简体   繁体   中英

How to return empty json array in asp.net core

In ASP.NET Core 2.0 Web API, when an object is empty, it will return a JSON array with null values like this:

{
    "code": "1",
    "message": null,
    "data": {
        "formSettings": [
            null,
            null
        ]
    }
}

How can I return a response as shown in the below code, such that the JSON array has empty objects:

{
    "code": "1",
    "message": null,
    "data": {
        "formSettings": []
    }
}

This is best way to send the empty json.
{ "code": "1", "message": null, "data": { "formSettings": [] } }

I could return empty json when add below code to Startup.cs

services.AddMvc()
             .AddJsonOptions(options => {
                 options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
             });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM