Converting list of objects to json array

前端 未结 3 1796
余生分开走
余生分开走 2021-01-02 07:31

I have a List of class objects that have email address and status data members. I am trying to convert these to a json, making sure to have the \"operations\" word on the ar

相关标签:
3条回答
  • 2021-01-02 08:13

    I believe this will give you what you want. You will have to change your class property names if possible -

            class MyClass
            {
                 public string email_address { get; set; }
                 public string status { get; set; }
            }
    
            List<MyClass> data = new List<MyClass>() { new MyClass() { email_address = "email1@email.com", status = "good2go" }, new MyClass() { email_address = "email2@email.com", status = "good2go" } };
            var json = JsonConvert.SerializeObject(new
            {
                operations = data
            });
    
    0 讨论(0)
  • 2021-01-02 08:19

    You can try with something like this:

    using System.Web.Script.Serialization;
    var jsonSerialiser = new JavaScriptSerializer();
    var json = jsonSerialiser.Serialize(data);
    
    0 讨论(0)
  • 2021-01-02 08:21
    class MyClass
    {
         public string email_address { get; set; }
         public string status { get; set; }
    }
    
    List<MyClass> data = new List<MyClass>() { new MyClass() { email_address = "email1@email.com", status = "good2go" }, new MyClass() { email_address = "email2@email.com", status = "good2go" } };
    
    //Serialize
    var json = JsonConvert.SerializeObject(data);
    
    //Deserialize
    var jsonToList = JsonConvert.DeserializeObject<List<MyClass>>(json);
    
    0 讨论(0)
提交回复
热议问题