Newtonsoft.Json序列化和反序列

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

  1. Product product = new Product();
  1.  
  1. product.Name = "Apple";
  1. product.Expiry = new DateTime(2008,12,28);
  1. product.Price = 3.99M;
  1. product.Sizes = new string[] { "Small","Medium","Large" };
  1.  
  1. string output = javascriptConvert.SerializeObject(product);
  1. //{
  1. // "Name": "Apple",
  1. // "Expiry": new Date(1230422400000),
  1. // "Price": 3.99,
  1. // "Sizes": [
  1. // "Small",
  1. // "Medium",
  1. // "Large"
  1. // ]
  1. //}
  1.  
  1. Product deserializedProduct = (Product)javascriptConvert.DeserializeObject(output,typeof(Product));

读取JSON

  1. string jsonText = "['JSON!',1,true,{property:'value'}]";
  1.  
  1. JsonReader reader = new JsonReader(new StringReader(jsonText));
  1.  
  1. Console.WriteLine("TokenType\t\tValueType\t\tValue");
  1.  
  1. while (reader.Read())
  1. {
  1. Console.WriteLine(reader.TokenType + "\t\t" + WriteValue(reader.ValueType) + "\t\t" + WriteValue(reader.Value))
  1. }
  2.  

结果显示:
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();
  1. JsonWriter writer = new JsonWriter(sw);
  1.  
  1. writer.WriteStartArray();
  1. writer.WriteValue("JSON!");
  1. writer.WriteValue(1);
  1. writer.WriteValue(true);
  1. writer.WriteStartObject();
  1. writer.WritePropertyName("property");
  1. writer.WriteValue("value");
  1. writer.WriteEndObject();
  1. writer.WriteEndArray();
  1.  
  1. writer.Flush();
  1.  
  1. string jsonText = sw.GetStringBuilder().ToString();
  1.  
  1. Console.WriteLine(jsonText);
  1. // ['JSON!',{property:'value'}]

这里会打印出: ['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. }

猜你在找的Json相关文章