Asp.Net中使用Newtonsoft.Json转换,读取,写入
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
//把Json字符串反序列化为对象
目标对象 = JsonConvert.DeserializeObject(JSON字符串, typeof(目标对象));
//把目标对象序列化为Json字符串
string Json字符串 = JsonConvert.SerializeObject(目标对象);
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 = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "Expiry": new Date(1230422400000),
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = (Product)JsonConvert.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))
}
结果显示:
| 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写入
StringWriter sw = new StringWriter();
JsonWriter writer = new JsonTextWriter(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'}]
//把datatable转换成json格式
public string GetAllCategory()
{
string result = "";
DataTable dt= catDAO.GetAllCategory();
result=JsonConvert.SerializeObject(dt, new DataTableConverter());
return result;
}
Asp.Net中使用Newtonsoft.Json转换,读取,写入的更多相关文章
- 在.NET中使用Newtonsoft.Json转换,读取,写入的方法介绍
全局引用 using Newtonsoft.Json; using Newtonsoft.Json.Converters; //把Json字符串反序列化为对象 目标对象 = JavaScriptCon ...
- c# 在.NET使用Newtonsoft.Json转换,读取,写入json
转自:http://blog.sina.com.cn/s/blog_70686f3a0101kemg.html 首先,大家要明白什么是json,了解更多关于json方面资料大家可以点击https:/ ...
- 在.NET使用Newtonsoft.Json转换,读取,写入json
首先,大家要明白什么是json,了解更多关于json方面资料大家可以点击https://www.ibm.com/developerworks/cn/web/wa-lo-json/ ,我在这里简单介绍下 ...
- Asp.Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty
public class NullToEmptyStringResolver : DefaultContractResolver { /// <summary> /// 创建属性 /// ...
- SQL中采用Newtonsoft.Json处理json字符串
原文 SQL中采用Newtonsoft.Json处理json字符串 使用环境: SQL Server2005; VS2010; 关于SQL中部署CLR程序集的方法,网上一搜一大把,需要了解的自行查阅, ...
- ASP.NET中XML转JSON的方法
原文:ASP.NET中XML转JSON的方法 许多应用程序都将数据存储为XML的格式,而且会将数据以JSON的格式发送到客户端以做进一步处理.要实现这一点,它们必须将XML格式转换为JSON格式. X ...
- Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案
使用Newtonsoft.Json 转换DateTime类型时,若使用标准转换,则字符串内会有一个T(虽然再转换成DateTime没有问题). 若要转换成DateTime没有T,可以加上特性: pub ...
- Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty
原文:Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty public class NullToEmptyStringResolver : De ...
- 在 .NET Core 3.0 中支持 Newtonsoft.Json 的使用
.NET Core 3.0 已经使用了一整套内置的 Josn 序列化/反序列化方案,而且看上去效率还不错.但对于某些项目必须使用到 Newtonsoft.Json 的时候,就会抛出如下异常: Syst ...
随机推荐
- 在Ubuntu系统下设置永久性Swap交换空间(转帖)
http://blog.sina.com.cn/s/blog_6e4388910100tsk7.html swap 一般都在一个专有的swap区里,这可能是因为在安装的时候系统会提示你创建一个swap ...
- 072——VUE中vuex之使用mutations修改购物车仓库数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PHP:第三章——PHP中的回调函数
<?php header("Content-Type:text/html;charset=utf-8"); //回调函数 //计算两个数只和 function Add($a, ...
- POJ 1008 简单模拟题
e.... 虽然这是一道灰常简单的模拟题.但是米做的时候没有读懂第二个日历的计时方法.然后捏.敲完之后华丽的WA了进一个点.坑点就在一年的最后一天你是该输出本年的.e ...但是我好想并没有..看di ...
- Python3.5+SQL+Prometheus+Grafana报表/监控
参考资料: pymysql 单独获取表的栏位名称 pymysql返回数据为字典形式(key:value--列:值) 行列结合,作为prometheus_client的输出. 话不多说,直接上脚本. ...
- EhLib的行Checkbox
方法1 http://www.cnblogs.com/jupt/p/4291902.html 在Indicator中添加动态Checkbox,无需绑定数据源,支持全选 - Ehlib学习(二) 先 ...
- C# 使用cmd输入参数来执行控制台应用程序
在外部可以使用cmd命令向C#控制台应用程序发送参数,并使之处理.main函数的形参一定要包含string[] args,否则该控制台应用程序不能接收外部参数.在使用cmd调用程序的时候,外部每个参数 ...
- 使用pool的多进程,不执行的问题
from multiprocessing import Pool def fetch_data(idlist,test): pass p=Pool(4) result=[] for i in rang ...
- centos7 sftp设置后 ssh 启动失败 原因分析
大多数人 在 设置SFTP 使用时,会在 ../sshd_config中添加如下内容: ------------------------------- Subsystem sftp internal- ...
- 集成学习之Boosting —— XGBoost
集成学习之Boosting -- AdaBoost 集成学习之Boosting -- Gradient Boosting 集成学习之Boosting -- XGBoost Gradient Boost ...