Click here to Skip to main content
15,891,684 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
**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#


What I have tried:

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();
Posted
Updated 3-Mar-19 17:23pm
v2
Comments
Graeme_Grant 3-Mar-19 16:44pm    
So you want to change a JSON object properties from single values to use arrays?
Ruter11 3-Mar-19 21:10pm    
I need something like this:

{"Container":[
{"ContainerId":"1","ContainerName":"Container1"},
{"ContainerId":"2","ContainerName":"Container2"},
{"ContainerId":"3","ContainerName":"Container3"},
{"ContainerId":"4","ContainerName":"Container4"}
]
}

1 solution

I'm guessing this is what you looking for. The code is looking for an object to hold list of container.

I would suggest create couple of Objects
C#
public class Container
	{
		public string ContainerId { get; set; }
		public string ContainerName { get; set; }
	}

public class Dispatch
	{
		public Dispatch()
		{
			Container = new List<Container>();
		}
		public List<Container> Container { get; set; }
	}


below is an example on how to populate it
C#
//.... Your other code

//initialize the object
var Dispatch = new Dispatch();

foreach (var dispatchItem in packItemRequest.DispatchItems)
{
//.... Your other code
   var propertyDispatch = new Container();
   propertyDispatch.ContainerId = containerid.ToString();
   propertyDispatch.ContainerName = dispatchItem.Container.ToString()

   Dispatch.Container.Add(propertyDispatch);
//.... Your other code

}


Sample output (if implemented correctly :) ):
HTML
{
  "Container": [
    {
      "ContainerId": "123",
      "ContainerName": "gdfgfdgfd"
    },
    {
      "ContainerId": "222",
      "ContainerName": "jhgj,ljkljl"
    }
  ]
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900