I’m trying to return a dynamic json array to the client side in mvc.
So far I have
var a = 1;
var b = 10;
var jsonArray = new JArray();
for (var i = 1; i < 5; i++)
{
var json = new JObject();
json.Add("field" + a, b);
jsonArray.Add(json);
a++;
b++;
}
return Json(jsonArray);
this returns to the client
[[[[]]]]
I have tried converting the JsonArray to a string first and setting it to have no formatter, but that doesn’t return valid json according to fiddler.
I’d expect the result to be soemething like:
[{field1:10},{field2:11},{field3:12}]
Can anyone point out what I’m doing wrong
This passed muster in Fiddler:
Fiddler seems to need JSON objects in the form
{ "FieldName": value }, hence my creation of an anonymous object. You could use any name in place ofJsonArray.Simply returning
Json(jsonArray)isn’t going to work becausejsonArraywill have an underlying representation that is dissimilar to your desired output, hence the output you are seeing when you serialize it.