Search code examples
c#jsonrestasp.net-web-api

Square brackets around response JSON in Web API


I have built a C# Web API using Swagger. It's up and running at this link: https://www.vijilcomm.com/api/swagger/index.html.

When I hit it, for example like so:

https://www.vijilcomm.com/api/VijilconOn?SerialNumber=12345&IPAddress=99.41.190.178

The response I get back has square brackets around it.

[
    {
        "serialNumber": "12345",
        "ipAddress": "99.41.190.178",
        "heartbeatWait": 0,
        "alertSoundTime": 0,
        "fullBatteryLevel": 0,
        "middleBatteryLevel": 0,
        "lowBatteryLevel": 0,
        "criticalBatteryLevel": 0,
        "tooHardMovement": 0,
        "unitID": "",
        "reportPhone": "",
        "action": "Device not found"
    }
]

I know in JSON square brackets indicate an array. It looks like it's outputting it as an array or something along those lines.

Is this valid JSON with the square brackets around the response, or is there something wrong with my code?

My controller has this code:

[HttpGet(Name = "VijilconOn")]
public IEnumerable<VijilconOn> Get(string SerialNumber, string IPAddress)
{
     return Enumerable.Range(1, 1).Select(index => new VijilconOn
     {
         HeartbeatWait = (Functions.ReturnIntFieldValue("HeartbeatWait", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
         AlertSoundTime = (Functions.ReturnIntFieldValue("AlertSoundTime", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
         FullBatteryLevel = (Functions.ReturnIntFieldValue("FullBatteryLevel", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
         MiddleBatteryLevel = (Functions.ReturnIntFieldValue("MiddleBatteryLevel", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
         LowBatteryLevel = (Functions.ReturnIntFieldValue("LowBatteryLevel", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
         CriticalBatteryLevel = (Functions.ReturnIntFieldValue("CriticalBatteryLevel", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
         TooHardMovement = (Functions.ReturnIntFieldValue("TooHardMovement", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
         UnitID = Functions.ReturnFieldValue("FriendlyName", "Devices", "[SerialNumber]='" + SerialNumber + "'").ToString(),
         ReportPhone = Functions.ReturnFieldValue("ReportPhone", "Devices", "[SerialNumber]='" + SerialNumber + "'").ToString(),
         IPAddress = Functions.RecordIPAddress(SerialNumber,IPAddress).ToString(),
         Action = Functions.CheckIn(SerialNumber, "ON").ToString(),
         SerialNumber = SerialNumber
     })
     .ToArray();
}

Which seems to make sense to me that it's being output as an array. My question is - am I doing something wrong here, or is OK that the JSON is being output in this way?

Thank you for your help! This is my first REST JSON Web API, so I'm not sure if I'm on the right track here, or if something is amiss. Thank you!

I tried using the code above and swagger, and the JSON was output as described. I'm not sure if I have to change how my controller function is behaving or if I can just leave it as is...


Solution

  • If you want the response as an array, your code looks fine.

    But if you want to return a single object, you don't have to use IEnumerable. Instead of creating Enumerable.Range(1,1), you can create an object and return it.

     [HttpGet(Name = "VijilconOn")]
     public VijilconOn Get(string SerialNumber, string IPAddress)
     {
         return new VijilconOn
         {
             HeartbeatWait = (Functions.ReturnIntFieldValue("HeartbeatWait", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
             AlertSoundTime = (Functions.ReturnIntFieldValue("AlertSoundTime", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
             FullBatteryLevel = (Functions.ReturnIntFieldValue("FullBatteryLevel", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
             MiddleBatteryLevel = (Functions.ReturnIntFieldValue("MiddleBatteryLevel", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
             LowBatteryLevel = (Functions.ReturnIntFieldValue("LowBatteryLevel", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
             CriticalBatteryLevel = (Functions.ReturnIntFieldValue("CriticalBatteryLevel", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
             TooHardMovement = (Functions.ReturnIntFieldValue("TooHardMovement", "Devices", "[SerialNumber]='" + SerialNumber + "'")),
             UnitID = Functions.ReturnFieldValue("FriendlyName", "Devices", "[SerialNumber]='" + SerialNumber + "'").ToString(),
             ReportPhone = Functions.ReturnFieldValue("ReportPhone", "Devices", "[SerialNumber]='" + SerialNumber + "'").ToString(),
             IPAddress = Functions.RecordIPAddress(SerialNumber,IPAddress).ToString(),
             Action = Functions.CheckIn(SerialNumber, "ON").ToString(),
             SerialNumber = SerialNumber
         };
     }