I am developing an application that will run on a tablet/phone and use Web API calls to get and update data on the backend.
I prefer to code my APIs versus using the VS dev environment (I like to have control over my code) so on the backend I have implemented a self host Web API. I will eventually move it to a service but for now it is a console application (it makes debugging much easier).
On the front end, my application calls the web APIs to get the data that it needs and to also create new records on the backend.
Up until now, it has been very straightforward and everything is working but I am stuck on how to get a list back from the WebAPI.
In my application, I want to be able to present a list of codes to the user. I will call the API, get the list back, put it into a listbox and allow the user to select. Once they select one I will use its ID to record the data on the backend. I have figured out how to create a return the list on the backend. The problem is that the data being returned isn't formatted correctly so the front end JSON doesn't know how to parse the string and breaks.
Here is the code that I am using on the backend to generate the list:
Public Function GetGetRejectCodes(id As String) As List(Of RejectCode) Return GetRejectCodes(id) End Function
GetRejectCodes just creates a simple list with each record being an id (integer) and a description (string).
On the front end, this is what I receive back:
[{"iID":1,"sReason":"Incorrect Length"},{"iID":2,"sReason":"Poor Quality"},{"iID":3,"sReason":"Wrong Location"},{"iID":4,"sReason":"Measurement Error"},{"iID":5,"sReason":"Orientation"}]
The data is correct. When the program goes to deserialize the data, I get this error:
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'QA_Test_Application.JSON+RejectCodes' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
I have written a similar program that gets its data from a different backend (running on Linux). The string that I get back from it specifically says that the data is a list and deserializes it correctly. I am pretty sure that the string above needs to look like this:
{"list":[{"iID":1,"sReason":"Incorrect Length"},{"iID":2,"sReason":"Poor Quality"},{"iID":3,"sReason":"Wrong Location"},{"iID":4,"sReason":"Measurement Error"},{"iID":5,"sReason":"Orientation"}]}
In my program I am cheating right now and doing this after I get the result back from the backend:
res = "{""list"":"+ res + "}"
When I do this the JSON is deserialized correctly and I get what I need.
There has to be a way to tell the backend WebAPI that what it is returning is a list and to format it correctly.
Any ideas?
John