json序列化和反序列化的使用教程

实体

public class wendaModel
{
private string _title;
private string _cons; public string title
{
set { _title = value; }
get { return _title; }
}
public string cons
{
set { _cons = value; }
get { return _cons; }
}
}

1、对象转换为json字符串(序列化)

wendaModel model = new wendaModel();
model.title = "json";
model.cons = "asp.net"; string json = Newtonsoft.Json.JsonConvert.SerializeObject(model);
Response.Write(json);
//输入结果:{"title":"json","cons":"asp.net"}

2、List转换为json字符串(序列化)

List<wendaModel> list = new List<wendaModel>();
for (int i = ; i < ; i++)
{
wendaModel model = new wendaModel();
model.title = "json" + i.ToString();
model.cons = "asp.net" + i.ToString();
list.Add(model);
}
string json = Newtonsoft.Json.JsonConvert.SerializeObject(list);
Response.Write(json);
//输出结果:[{"title":"json0","cons":"asp.net0"},{"title":"json1","cons":"asp.net1"},{"title":"json2","cons":"asp.net2"}]

3、json字符串转换为对象(反序列化)

string json = "{\"cons\":\"asp.net\",\"title\":\"json\"}";
wendaModel model = Newtonsoft.Json.JsonConvert.DeserializeObject<wendaModel>(json);
Response.Write(model.title + "=" + model.cons);
//输出结果:json=asp.net

4、json字符串转换为对象(反序列化)

string json = "[{\"cons\":\"asp.net2\",\"title\":\"json2\"},{\"cons\":\"asp.net1\",\"title\":\"json1\"}]";
List<wendaModel> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<wendaModel>>(json);
foreach (var item in list)
{
Response.Write(item.title);
}

Asp.Net Newtonsoft.Json使用教程的更多相关文章

  1. asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析

    下面我用一个实例来和大家分享一下我的经验,asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析. using Newtonsoft.Json; usin ...

  2. 基于 Vue.js 之 iView UI 框架非工程化实践记要 使用 Newtonsoft.Json 操作 JSON 字符串 基于.net core实现项目自动编译、并生成nuget包 webpack + vue 在dev和production模式下的小小区别 这样入门asp.net core 之 静态文件 这样入门asp.net core,如何

    基于 Vue.js 之 iView UI 框架非工程化实践记要   像我们平日里做惯了 Java 或者 .NET 这种后端程序员,对于前端的认识还常常停留在 jQuery 时代,包括其插件在需要时就引 ...

  3. 在Asp.Net Core 3.0中如何使用 Newtonsoft.Json 库序列化数据

    在.Net Core 3.0中 内置了一套Json序列化/反序列化方案,默认可以不再依赖,不再支持   Newtonsoft.Json. 但是.NET Core 3.0 System.Text.Jso ...

  4. asp.net core 3.0 JObject The collection type 'Newtonsoft.Json.Linq.JObject' is not supported

    在asp.net core 3.0 中,如果直接在Controller中返回 Jobject 类型,会抛出如下错误: The collection type 'Newtonsoft.Json.Linq ...

  5. .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程

    JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...

  6. ASP.NET JSON的序列化和反序列化 之 Newtonsoft.Json

    我们用到的类库为:Newtonsoft.Json,通过VS工具中NuGet程序包可以下载. 一:对象转json-序列化 public class Student { public int ID { g ...

  7. Asp.Net中使用Newtonsoft.Json转换,读取,写入

    using Newtonsoft.Json;using Newtonsoft.Json.Converters; //把Json字符串反序列化为对象目标对象 = JsonConvert.Deserial ...

  8. ASP.NET2.0 Newtonsoft.Json 操作类分享

    JSON 是现在比较流行的数据交互格式,NET3.0+有自带类处理JSON,2.0的话需要借助Newtonsoft.Json来完成,不然自己写的话,很麻烦. 网上搜索下载 Newtonsoft.Jso ...

  9. 使用Newtonsoft.json 解决 Asp.Net MVC DateTime类型数据Json格式化问题

    解决思路 众所周知,MVC中调用的微软的组件JavaScriptSerialer...,格式DateTime类型数据需要在客户端专门解. 还知道,NewtonSoft.json可以“正确”的格式化Da ...

随机推荐

  1. React + antd 组件离开页面以后出现Can only update a mounted or mounting component 的解决办法

    做项目的过程中,来回切换页面时,一直遇到Can only update a mounted or mounting component 这个问题,原因是当离开页面以后,组件已经被卸载,执行setSta ...

  2. win 2012 安装Net35

    使用 PowerShell, 指定源文件路径然后进行安装: Install-WindowsFeature NET-Framework-Core –Source D:\Sources\sxs 使用命令提 ...

  3. 解决WPF导入图片不显示的问题

    想在XAML中使用一张图片,得先将其添加到工程中, 方法是: 在项目中双击Resources.resx,选择图像,在添加资源的下拉菜单中选择添加现有文件,然后选择文件,添加图片进来后可以在Resour ...

  4. 打造利器Qt Creator:代码todo工具的使用

    http://blog.csdn.net/maobush/article/details/67636734

  5. CC2431 代码分析②-CC2431狂轰滥炸

    CC2431 code review : CC2431 狂轰滥炸 在上一篇中的最后我们分析到CC2431 开始喊出第一声,这里我们逐步分析从第一声到后面的狂轰滥炸! 上代码 /************ ...

  6. [Vijos1130][NOIP2001]数的计数 (递推)

    自己的递推一塌糊涂 考前抱佛脚 #include<bits/stdc++.h> using namespace std; ]; int main() { int n;scanf(" ...

  7. [P1306] 斐波那契公约数 (矩阵快速幂+斐波那契数列)

    一开始数据没加强,一个简单的程序可以拿过 gcd(f[n],f[m])=f[gcd(n,m)] 下面这个是加强数据之后的80分代码 #include<bits/stdc++.h> usin ...

  8. 寻找总是出错的R文件

    寻找R文件 相信大家使用Android studio开发APP时最常见的一个问题就是R文件爆红了·· 接下来我们来看看怎么寻找R文件 首先我们得从Android转换成Project. 然后就是依次点击 ...

  9. 变量安全过滤,防止xss攻击

    下面这个方法不管是字符串还是数组,都可以进行过滤 /** * @purpose : 对变量进行安全过滤,使 $_GET.$_POST.$q->record 等变量更安全 * @author : ...

  10. Git删除本地修改

    git如何删除本地所有未提交的更改 git checkout . && git clean -xdf 一般 git clean都是配合git reset 使用的 如果你有的修改已经加入 ...