Reputation: 707
I get the json from server, add JObject to the JArray. I want to set a name to that json. I do not know how to do it.
foreach(var i = 0; i < 2; i++)
{
var array = new JArray();
var jsonFromServer = GetDataFromServer();
var strAsJson = JsonConvert.DeserializeObject<JObject>(jsonFromServer);
array.Add(strAsJson);
}
I am having now:
[
{...},
{...},
{...}
]
My task is:
[
"0": {...},
"1": {...},
"2": {...}
]
I can not to set the name to the JObject. I can to add the name to the JProperty, but I can not to add JProperty to JArray, because it is not a JObject. I can use the JObject instead and add the json to JsonProperty, but I have to use the JArray.
Upvotes: 0
Views: 379
Reputation: 3576
Try this method:
var obj = new JObject();
obj.Add($"{i}", strAsJson);
array.Add(obj);
Or this handy one-liner:
array.Add(new JObject(new JProperty($"{i}", JsonConvert.DeserializeObject(jsonFromServer))));
Upvotes: 2