I'm building a dashboard (in a .NET MVC project) that basically receives data through an api and displays it in a (somewhat) fancy view.
The data I receive is formatted in json and I know the format of the "root" (not sure if this is the correct term when talking about json, feel free to correct me) which is something along those lines :
{
response :
{
fields :
[{
name : name1,
},
{
name : name2,
},
...
],
results :
[{
name1 : value1,
name2 : value2
},
{
name1 : value3,
name2 : value4
},
...
]
}
}
As you can see, the array 'results' varies from one response to the next, with the names 'name1' and 'name2' being user-generated (i.e. unpredictable). Now I can deserialize the json to extract the 'fields' array and its values using this model :
namespace WebApplication1.Models
{
public class Field
{
public string name { get; set; }
}
public class Result
{
}
public class JsonResponse
{
public List<Field> fields { get; set; }
public List<Result> results { get; set; }
}
}
My problem is :
when I pass this to the view, the 'results' array is filled with empty 'Objects' (which feels normal as they are not described in the model)
results: Array(5)
0: {}
1: {}
2: {}
3: {}
4: {}
My goal is :
I want to be able to add the correct description of 'results' to the model (that I can get from the 'fields' array) before passing it to the view, so that all the data is available in the view.
Things that have tried :
sending the Json as a string to the view and parse it with JavaScript but that doesn't work since we use a model-dependant engine to display our data
using a dynamicObj to decode the json, but I couldn't pass it to the view (as it is not a model)
crying, but it (surprinsingly) didn't work either.
probably some other thing I'm forgetting at this time.
I have been scouring the web for days for a solution, and I'm getting desperate, any ideas are welcome.
Thank you !
So, to anyone looking for this in the future, I ended up using a workaround as I didn't find a solution to this problem.
Instead of having a dynamic model, I rewrote the 'Result' class as follow :
that I can fill as I receive the data from the API.
It makes for slightly more annoying data handling in the view, but it's really not that bad.
P.s. I'm not marking this as answer as it is just a nice workaround