Как вставить значение в объект json в существующий ключ в виде массива?
**Below code output**: I am able to get json as below by this code { "ContainerId": "848dc1ca-04ae-457e-b6ac-0dee454816c4", "ContainerName": "Container1" } **Needed Output**: But I want to insert value dynamically like below json in array that contains multiple container ID and Container Name I have to make json like this {"Container":[{"ContainerId":"1","ContainerName":"Container1"}, {"ContainerId":"2","ContainerName":"Container2"}, {"ContainerId":"3","ContainerName":"Container3"}, {"ContainerId":"4","ContainerName":"Container4"}]} This is the code which for reference, Please let me know how I can do this in C#
Что я уже пробовал:
var jsonDiaptchObject = JObject.Parse(stockItem.Payload); var oldDispatchItem = await _stockItemService.GetStockItemFor(stockItem.Identifier); foreach (var dispatchItem in packItemRequest.DispatchItems) { containerid = Guid.NewGuid(); KeyValuePair<string, string>[] propertyDispatchs = new KeyValuePair<string, string>[] { new KeyValuePair<string, string>("ContainerId", containerid.ToString()), new KeyValuePair<string, string>("ContainerName", dispatchItem.Container.ToString()) }; string olddispatchValue = null; foreach (var propertyDispatch in propertyDispatchs) { if (jsonDiaptchObject.ContainsKey(propertyDispatch.Key)) { olddispatchValue = JObject.Parse(stockItem.Payload)[propertyDispatch.Key]?.ToString(); //have to get dispatch payload //jsonDiaptchObject.Add(propertyDispatch.Key.Insert(0, propertyDispatch.Value)); } jsonDiaptchObject.Add(propertyDispatch.Key, propertyDispatch.Value); } } stockItem.Payload = jsonDiaptchObject.ToString();
Graeme_Grant
Итак, вы хотите изменить свойства объекта JSON с одиночных значений на использование массивов?
Ruter11
Мне нужно что-то вроде этого:
{"Контейнер":[
{"ContainerId":"1","ContainerName":"Container1"},
{"ContainerId":"2","ContainerName":"Container2"},
{"ContainerId":"3","ContainerName":"Container3"},
{"ContainerId":"4","Из Имя_контейнера":"Контейнер4"}
]
}