Why is my entire object serialized as an Array when the DataMember Name is "length"

I created a new ASP.NET MVC 4 web application and changed the default ValuesController value:

using System.Runtime.Serialization;
using System.Web.Http;

namespace WebSerializationTest.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public ApiResponse Get()
        {
            ApiResponse res = new ApiResponse();
            res.Length = 120;
            return res;
        }
    }

    [DataContract]
    public class ApiResponse
    {
        [DataMember(Name = "length")]
        public int Length { get; set; }
    }
}

Now, when I make a request from the browser (with setting headers for Accept: application / json), I get this JSON:

[
    120
]

However , when I change the name of DataMember to something else, even [DataMember (Name = "Length")] I get the correct JSON:

{
    "Length" : 120
}

Is the string length forbidden or what causes this behavior?

Btw. I have a target environment installed on the .NET Framework 4, but I also tried the .NET Framework 4.5.1 and the problem is still there.

+4
source share
1 answer

, Firefox- RESTClient, .

() JSON , (Raw) JSON .

, , ​​ 2012 .

+2

Source: https://habr.com/ru/post/1598220/


All Articles