2013-05-05 88 views
0

我需要使用Javascript串行器生成以下JSON文件,JSON文件生成使用Javascript串行

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

你在使用什麼語言? – shakib 2013-05-05 19:06:33

回答

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"}] 
}