使用Javascript序列化程序生成Json文件

时间:2013-05-05 17:36:13

标签: json jira javascriptserializer

我需要使用Javascript序列化程序生成以下json文件,

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

1 个答案:

答案 0 :(得分:0)

我必须使用以下编码来实现这种情况,

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);

结果如下

{
  "components":[{"name": "AA"}],
  "customfield_10222":[{"name":"XXX"},{"name":"YYY"}]
}