2014-10-10 33 views
0

我具有以下陣列等創建基於陣列的JSON文件值

var Device = { 
    'iPhone 6 Plus-Simulator-iOS 8.0': 'iPhone 6 Plus (8.0 Simulator)', 
    'iPhone 6-Simulator-iOS 8.0': 'iPhone 6 (8.0 Simulator)', 
    'iPhone 5s-Simulator-iOS 8.0': 'iPhone 5s (8.0 Simulator)', 
    'iPhone 5s-Simulator-iOS 7.1': 'iPhone 5s (7.1 Simulator)', 
    'iPhone 5s-Simulator-IOS 7.0.3': 'iPhone 5s (7.0.3 Simulator)', 

}; 

我需要創建關於上述信息的json文件。 我怎麼能創建json文件與keyvalue

回答

0

您可以創建一個對象與鍵和值參數。

 public class Device 
     { 
     public Device(){} 
     public string Key {get;set;} 
     public string Value {get;set;} 
     } 

List<Device> deviceList = new List<Device>(); 
deviceList.Add(new Device() {Key = "", Value = "" }) 

然後使用串行化器來創建JSON文件(例如Newtonsoft.JSON):

string json = JsonConvert.SerializeObject(deviceList);