I am trying to add below JSON in VAR as an array.
[
{
"path": "test",
"op" : "replace",
"from" : "null",
"value" :"7c45f39c-708b-4d54-8fff-421640dyuuii"
}
]
I did try, below and it didn’t work me, how can declare this as an array, TIA.
var data = new
{
path="test",
op ="replace",
from = "null",
value ="7c45f39c-708b-4d54-8fff-421640dyuuii"
};
>Solution :
The syntax new { ... }
declares an object. You can use new[] { }
to declare an array. And then within that declaration you can declare you object(s). For example:
var data = new[] {
new {
path = "test",
op = "replace",
from = "null",
value = "7c45f39c-708b-4d54-8fff-421640dyuuii"
}
};
As an aside… Note that with implicit types you can’t declare an empty array, such as:
var data = new[] { };
As the compiler would need an example of the object to infer the type. In the example above the compiler infers an anonymous type from the inner new { ... }
declaration of your object. Any type would work, there just needs to be one. Otherwise you’d need to explicitly define a type. For example:
var data = new string[] {};