I'm working in .Net Core 2.2 with the MVC pattern. I have a Web-API controller, where I created some Endpoints with classic CRUD-methods.
Now I have everything compiling and when I debug my soluting I get an array containing a couple of JSON-formatted objects - but the system sees the output it as an array, not as JSON.
Now I want this array to become a JSON.
I've looked at this: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm
My orginal method looked like this (in controller):
[HttpGet("/ListAllItems")]
public async Task<IEnumerable<DtoModel>> ListAllItemsFromDb()
{
return await _dbProvider.ListAllItemsAsync();
}
and gave an output of:
[{"id": "GUID-STRING", "itemName": "foo"}, {"id": "GUID-STRING2", "itemName": "bar"}]
//My frontend did not recognize this as a JSON, since it is an array, and threw an exception
So I tried this in my controller instead
[HttpGet("/ListAllItems")]
public async Task<JObject> ListAllItemsFromDb()
{
var result = await _dbProvider.ListAllItemsAsync();
string output = result.ToString();
string json = JsonConvert.SerializeObject(output);
JObject obj = JObject.Parse(json);
return obj;
}
When I run this code, my error message states: "JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 60."
How do I make the array of objects into a JSON object?