How to create JSON string in C#

后端 未结 14 1041
闹比i
闹比i 2020-11-22 12:39

I just used the XmlWriter to create some XML to send back in an HTTP response. How would you create a JSON string. I assume you would just use a stringbuilder to build the

14条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 13:35

    Using Newtonsoft.Json makes it really easier:

    Product product = new Product();
    product.Name = "Apple";
    product.Expiry = new DateTime(2008, 12, 28);
    product.Price = 3.99M;
    product.Sizes = new string[] { "Small", "Medium", "Large" };
    
    string json = JsonConvert.SerializeObject(product);
    

    Documentation: Serializing and Deserializing JSON

提交回复
热议问题