Question

I need to generate the following json files using Javascript serializer,

1. {"components":[{"name":"AA"}]}
2. {"customfield_10222":[{"name":"xxx"},{"name":"yyyy"}]}  // this custom field represents the additional notification persons.
Was it helpful?

Solution

I have to achieve this scenario using the below coding,

public List<AdditionalUsers> AdditionalNotification = new List<AdditionalUsers>();
public List<ComponentsDetails> Component = new List<ComponentsDetails>();

  class AdditionalUsers
    {
        public string name;
    }

    class ComponentsDetails
    {
        public string name;
    }


string[] a=new string[2]{"XXX","YYY"};

 foreach (string additionalUser in a)
                                {
                                    AdditionalNotification.Add(new AdditionalUsers() { name =additionalUser });
                                }



 Component.Add(new ComponentsDetails() { name = "AA" });

var subFields = new Dictionary<string, object>();
subFields.Add("components", Component); // represents 1 json file
subFields.Add("customfield_10222", AdditionalNotification); // represents 2 json file

JavaScriptSerializer serializer = new JavaScriptSerializer();
            string json = serializer.Serialize((Object)subFields);
Console.WriteLine(json);

The result like this

{
  "components":[{"name": "AA"}],
  "customfield_10222":[{"name":"XXX"},{"name":"YYY"}]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top