Newtonsoft.Json序列化和反序列
来源:互联网 发布:huaweinexus网络 编辑:程序博客网 时间:2024/05/10 08:59
这里下载:http://www.newtonsoft.com/products/json/
安装:
1.解压下载文件,得到Newtonsoft.Json.dll
2.在项目中添加引用..
序列化和反序列在.net项目中:
读取JSON
结果显示:
TokenType ValueType Value StartArraynullnullStringSystem.StringJSON!IntegerSystem.Int321BooleanSystem.BooleanTrueStartObjectnullnullPropertyNameSystem.StringpropertyStringSystem.StringvalueEndObjectnullnullEndArraynullnull
安装:
1.解压下载文件,得到Newtonsoft.Json.dll
2.在项目中添加引用..
序列化和反序列在.net项目中:
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 output = javascriptConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "Expiry": new Date(1230422400000),
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = (Product)javascriptConvert.DeserializeObject(output, typeof(Product));
读取JSON
string jsonText = "['JSON!',1,true,{property:'value'}]";
JsonReader reader = new JsonReader(new StringReader(jsonText));
Console.WriteLine("TokenType\t\tValueType\t\tValue");
while (reader.Read())
{
Console.WriteLine(reader.TokenType + "\t\t" + WriteValue(reader.ValueType) + "\t\t" + WriteValue(reader.Value))
}
结果显示:
JSON写入
StringWriter sw = new StringWriter();
JsonWriter writer = new JsonWriter(sw);
writer.WriteStartArray();
writer.WriteValue("JSON!");
writer.WriteValue(1);
writer.WriteValue(true);
writer.WriteStartObject();
writer.WritePropertyName("property");
writer.WriteValue("value");
writer.WriteEndObject();
writer.WriteEndArray();
writer.Flush();
string jsonText = sw.GetStringBuilder().ToString();
Console.WriteLine(jsonText);
// ['JSON!',1,true,{property:'value'}]
这里会打印出: ['JSON!',1,true,{property:'value'}]
.
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Newtonsoft.Json;using System.IO;using Newtonsoft.Json.Linq;namespace WebApplication15{ public partial class WebForm4 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { var jsonBuilder = new System.Text.StringBuilder(); using (var jsonWriter = new Newtonsoft.Json.JsonTextWriter(new System.IO.StringWriter(jsonBuilder))) { jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("status"); if (false) { jsonWriter.WriteValue(false); jsonWriter.WritePropertyName("msg"); jsonWriter.WriteValue("未知原因,请重试"); } else { jsonWriter.WriteValue(true); jsonWriter.WritePropertyName("yftest"); jsonWriter.WriteStartArray(); for (int i = 0; i < 10; i++) { jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("id"); jsonWriter.WriteValue(i); jsonWriter.WritePropertyName("name"); jsonWriter.WriteValue("test"); jsonWriter.WriteEndObject(); } jsonWriter.WriteEndArray(); } jsonWriter.WriteEndObject(); jsonWriter.Flush(); } Response.Write(jsonBuilder); //获取根status //var jsonObject = Newtonsoft.Json.Linq.JObject.Parse(jsonBuilder.ToString()); //JToken ageToken = jsonObject["status"]; //Response.Write(ageToken.ToString()); //遍历深层参数 var jsonObject = Newtonsoft.Json.Linq.JObject.Parse(jsonBuilder.ToString()); var selectToken = jsonObject.SelectTokens("yftest", false); //if (selectToken != null && selectToken is Newtonsoft.Json.Linq.JArray) //{ foreach (var item in selectToken.Children()) { JToken ageToken = item["id"]; } //} } }}
0 0
- Newtonsoft.Json序列化和反序列
- Newtonsoft.Json序列化和反序列
- Newtonsoft.Json序列化和反序列
- Newtonsoft.Json序列化和反序列
- Newtonsoft.Json序列化和反序列
- Newtonsoft.Json序列化和反序列
- Newtonsoft.Json序列化和反序列
- Newtonsoft.Json序列化和反序列
- Newtonsoft.Json序列化和反序列
- Newtonsoft.Json序列化和反序列
- Newtonsoft.Json序列化和反序列
- Newtonsoft.Json序列化和反序列
- newTonSoft.Json进行序列化和反序列化
- Newtonsoft.Json的序列化和反序列化
- Newtonsoft.Json 序列化和反序列化
- ASP.NET:使用Newtonsoft.Json序列化和反序列化JSON对象的例子
- 利用Newtonsoft.Json序列化,反序列化,读取,写入
- Newtonsoft.Json.dll 中的序列化与反序列化
- HTTP请求中POST与GET的区别
- HTK tutorials
- 禁用<a>点击事件
- Linux网络配置
- Oracle Linux(64位)安装64位Oracle 11g遇到ins_ctx.mk问题
- Newtonsoft.Json序列化和反序列
- gdb调试有fork的程序
- tomcat启动在80端口 Permission denied <null>:80
- ubuntu14.04恢复系统默认中文字体
- j2ee
- DNS Query Flood攻击
- 处理 json 数据并生成 excle
- 素数测试(2)
- C/C++ 字符串 总结