Newtonsoft.Json序列化和反序列

前端之家收集整理的这篇文章主要介绍了Newtonsoft.Json序列化和反序列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这里下载:http://www.newtonsoft.com/products/json/
安装:
1 .解压下载文件,得到Newtonsoft.Json.dll
2.在项目中添加引用..
序列化和反序列在.net项目中:

  1. Product product = new Product();@H_403_21@
  2. @H_403_21@ 
  3.   
    product.Name = "Apple";@H_403_21@ 
  4.   
    product.Expiry = new DateTime(2008,12,28);@H_403_21@ 
  5.   
    product.Price = 3.99M;@H_403_21@ 
  6.   
    product.Sizes = new string[] { "Small","Medium","Large" };@H_403_21@ 
  7.   
    @H_403_21@ 
  8.   
    string output = javascriptConvert.SerializeObject(product);@H_403_21@ 
  9.   
    //{@H_403_21@ 
  10.   
    // "Name": "Apple",@H_403_21@ 
  11.   
    // "Expiry": new Date(1230422400000),@H_403_21@ 
  12.   
    // "Price": 3.99,@H_403_21@ 
  13.   
    // "Sizes": [@H_403_21@ 
  14.   
    // "Small",@H_403_21@ 
  15.   
    // "Medium",@H_403_21@ 
  16.   
    // "Large"@H_403_21@ 
  17.   
    // ]@H_403_21@ 
  18.   
    //}@H_403_21@ 
  19.   
    @H_403_21@ 
  20.   
    Product deserializedProduct = (Product)javascriptConvert.DeserializeObject(output,typeof(Product));@H_403_21@ 
  21.  

读取JSON

  1. string jsonText = "['JSON!',1,true,{property:'value'}]";@H_403_21@
  2. @H_403_21@ 
  3.   
    JsonReader reader = new JsonReader(new StringReader(jsonText));@H_403_21@ 
  4.   
    @H_403_21@ 
  5.   
    Console.WriteLine("TokenType\t\tValueType\t\tValue");@H_403_21@ 
  6.   
    @H_403_21@ 
  7.   
    while (reader.Read())@H_403_21@ 
  8.   
    {@H_403_21@ 
  9.   
     Console.WriteLine(reader.TokenType + "\t\t" + WriteValue(reader.ValueType) + "\t\t" + WriteValue(reader.Value))@H_403_21@ 
  10.   
    }
  11. @H_403_21@ 
  12.  

结果显示:
TokenType ValueType Value
StartArray null null
String System.String JSON!
Integer System.Int32 1
Boolean System.Boolean True
StartObject null null
PropertyName System.String property
String System.String value
EndObject null null
EndArray null null

JSON写入

  1. StringWriter sw = new StringWriter();@H_403_21@
  2. JsonWriter writer = new JsonWriter(sw);@H_403_21@ 
  3.   
    @H_403_21@ 
  4.   
    writer.WriteStartArray();@H_403_21@ 
  5.   
    writer.WriteValue("JSON!");@H_403_21@ 
  6.   
    writer.WriteValue(1);@H_403_21@ 
  7.   
    writer.WriteValue(true);@H_403_21@ 
  8.   
    writer.WriteStartObject();@H_403_21@ 
  9.   
    writer.WritePropertyName("property");@H_403_21@ 
  10.   
    writer.WriteValue("value");@H_403_21@ 
  11.   
    writer.WriteEndObject();@H_403_21@ 
  12.   
    writer.WriteEndArray();@H_403_21@ 
  13.   
    @H_403_21@ 
  14.   
    writer.Flush();@H_403_21@ 
  15.   
    @H_403_21@ 
  16.   
    string jsonText = sw.GetStringBuilder().ToString();@H_403_21@ 
  17.   
    @H_403_21@ 
  18.   
    Console.WriteLine(jsonText);@H_403_21@ 
  19.   
    // ['JSON!',{property:'value'}]@H_403_21@ 
  20.  

这里会打印出: ['JSON!',{property:'value'}].







  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using Newtonsoft.Json;
  8. using System.IO;
  9. using Newtonsoft.Json.Linq;
  10.  
  11. namespace WebApplication15
  12. {
  13. public partial class WebForm4 : System.Web.UI.Page
  14. {
  15. protected void Page_Load(object sender,EventArgs e)
  16. {
  17. var jsonBuilder = new System.Text.StringBuilder();
  18. using (var jsonWriter = new Newtonsoft.Json.JsonTextWriter(new System.IO.StringWriter(jsonBuilder)))
  19. {
  20. jsonWriter.WriteStartObject();
  21. jsonWriter.WritePropertyName("status");
  22.  
  23. if (false)
  24. {
  25. jsonWriter.WriteValue(false);
  26. jsonWriter.WritePropertyName("msg");
  27. jsonWriter.WriteValue("未知原因,请重试");
  28. }
  29. else
  30. {
  31. jsonWriter.WriteValue(true);
  32. jsonWriter.WritePropertyName("yftest");
  33. jsonWriter.WriteStartArray();
  34. for (int i = 0; i < 10; i++)
  35. {
  36. jsonWriter.WriteStartObject();
  37. jsonWriter.WritePropertyName("id");
  38. jsonWriter.WriteValue(i);
  39. jsonWriter.WritePropertyName("name");
  40. jsonWriter.WriteValue("test");
  41. jsonWriter.WriteEndObject();
  42. }
  43.  
  44. jsonWriter.WriteEndArray();
  45. }
  46.  
  47. jsonWriter.WriteEndObject();
  48. jsonWriter.Flush();
  49. }
  50.  
  51. Response.Write(jsonBuilder);
  52.  
  53. //获取根status
  54. //var jsonObject = Newtonsoft.Json.Linq.JObject.Parse(jsonBuilder.ToString());
  55.  
  56. //JToken ageToken = jsonObject["status"];
  57. //Response.Write(ageToken.ToString());
  58.  
  59. //遍历深层参数
  60. var jsonObject = Newtonsoft.Json.Linq.JObject.Parse(jsonBuilder.ToString());
  61. var selectToken = jsonObject.SelectTokens("yftest",false);
  62. //if (selectToken != null && selectToken is Newtonsoft.Json.Linq.JArray)
  63. //{
  64. foreach (var item in selectToken.Children())
  65. {
  66. JToken ageToken = item["id"];
  67. }
  68. //}
  69. }
  70. }
  71. }@H_403_21@

猜你在找的Json相关文章