如何在c#

时间:2016-03-09 12:07:01

标签: c# web-services arraylist

  1. 如何在c#中从另一个类中分配arraylist中的数据?
  2. 以下是我的课程。
  3. 我想序列化和反序列化数据,但不知道如何在arraylist中分配数据。
  4.  public class PlatanoJson  //this is first class where arraylist is declared  
     {  
        public string question { get; set; }  
        public string correctans { get; set; }  
        public bool typeofans { get; set; }  
        public int indexofque { get; set; }  
        public ArrayList DiffAns = new ArrayList();  
     }  
    
     public class JsonSerializationController : ApiController  
     {  
        [Route("getjsondata")]  
        public string jsonDataToSerialize()  
        {  
            var game = new PlatanoJson {  
                question = "What is your education?",  
                correctans = "MCA",  
                typeofans = true,  
                indexofque = 3,  
                DiffAns =   //how to add data here??  
            };  
    
            var result = Newtonsoft.Json.JsonConvert.SerializeObject(game);  
    
            var dresult = Newtonsoft.Json.JsonConvert.DeserializeObject<PlatanoJson>(result);  
    
            return (" Question="+dresult.question+" Correct Answer="+dresult.correctans+" Type of ans="+dresult.typeofans+" Index of Que"+dresult.indexofque+" Answers choice="+dresult.DiffAns);
        }
     }
    

2 个答案:

答案 0 :(得分:0)

由于ArrayList不对包含的数据做任何假设,您可以向其添加任何数据类型。为此,有几种可能性,例如,使用对象初始化器:

var game = new PlatanoJson {
    question = "What is your education?",
    correctans = "MCA",
    typeofans = true,
    indexofque = 3,
    DiffAns = new ArrayList { 1, 2, 3, "myString" }
 };

或者你也可以使用期望ICollection

的constrcutor
DiffAns = new ArrayList(new object[] { 1, 2, 3, "myString" })

但是我会建议随着时间的推移而使用egeneric appraoches。使用ArrayList,您无法限制列表中可能包含的对象,您可以(如上面的代码中所示)将inetegeres,字符串和任何不同的数据类型放入其中。更好地使用List<T>,它为您提供编译时类型安全性:

var game = new PlatanoJson {
    question = "What is your education?",
    correctans = "MCA",
    typeofans = true,
    indexofque = 3,
    DiffAns = new List<int>{ 1, 2, 3 }  // you van´t put a string into this list
 };

答案 1 :(得分:0)

为什么不将代码更改为以下内容(如果列表包含字符串)。

public class PlatanoJson  //this is first class where arraylist is     declared{
public string question { get; set; }
public string correctans { get; set; }
public bool typeofans { get; set; }
public int indexofque { get; set; }
public List<string> DiffAns { get; set; }
}


 public class JsonSerializationController : ApiController
  {
[Route("getjsondata")]
public string jsonDataToSerialize()
{
var list= new List<string>();
list.Add("your stuff1");
list.Add("your stuff2");
list.Add("your stuff3");
    var game = new PlatanoJson {
           question = "What is your education?",
        correctans = "MCA",
        typeofans = true,
        indexofque = 3,
        DiffAns = list;  
    };
        var result = Newtonsoft.Json.JsonConvert.SerializeObject(game);
    var dresult = Newtonsoft.Json.JsonConvert.DeserializeObject<PlatanoJson>        (......................
}

}