How to create JSON string in C#

后端 未结 14 1005
闹比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:33

    Take a look at http://www.codeplex.com/json/ for the json-net.aspx project. Why re-invent the wheel?

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题