Json详解3-Newtonsoft.Json(Json.net)的基本用法(转载)
原转载地址:
http://www.cnblogs.com/youring2/archive/2012/05/28/2520922.html
添加引用:
使用NuGet,命令:install-package Newtonsoft.Json
实体类:
public class Book { public string BookID { get; set; } public DateTime PublishDate { get; set; } public decimal Price { get; set; } public override string ToString() { return "ID:" + BookID + "; Date:" + PublishDate.ToShortDateString() + "; Price" + Price.ToString("n"); } }
序列化和反序列化:
Book bk = new Book() { BookID = "12111", PublishDate = DateTime.Parse("2012-2-1 22:12:11"), Price=433.12M}; Console.WriteLine(JsonConvert.SerializeObject(bk)); string jsonBook = "{'BookID':'123', 'PublishDate':'2011-1-2', 'Price':23.5}"; Book bk1 = JsonConvert.DeserializeObject<Book>(jsonBook); Console.WriteLine(bk1.ToString());
以下为个人实际操作情况:
一、环境vs2013
二、在已有的web 项目中添加页面
三、添加引用,我因为之前的项目中已经存在所以可以直接看到

四,直接添加一个web(aspx)页面
并将作者原来的代码直接拷入,界面如下 ,注意添加引用 using Netwtonsoft.Json;


Console.WriteLine 的语句不知道在哪显示,下面是一种办法,在google中没找到可以查看的地方,google浏览器的调试使用需要提高

执行后,Console.WriteLine直接执行,不能直接看到结果,按照上面查到的方法,添加一句 Console.ReadLine();
当然也可添加断点在调试环境下查看,就不再尝试
执行结果: Console.WriteLine(bk1.ToString()); 该语句一直不能正常调用,页面总是处于加载状态,但一直不能调用,正常显示
简单修改后,添加了两个控件用 来显示内容,以下语句执行成功;
aspx.cs代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Newtonsoft.Json; namespace WebApplication1.JsonTest { public class Book { public string BookID { get; set; } public DateTime PublishDate { get; set; } public decimal Price { get; set; } public override string ToString() { return "ID:" + BookID + "; Date:" + PublishDate.ToShortDateString() + "; Price" + Price.ToString("n"); } } public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //反序列化 string jsonBook = "{'BookID':'123', 'PublishDate':'2011-1-2', 'Price':23.5}"; Book bk1 = JsonConvert.DeserializeObject<Book>(jsonBook); //把字符串反序列化成实例,需要强制类型转换 p1.InnerText = "反序列化,输出时是实例的各个值 bk1.BookId:" + bk1.BookID + ";bk1.PublishDate:" + bk1.PublishDate.ToString() + ";bk1.Price:" + bk1.Price; //已经解析成各个字段,程序能识别, } protected void Button2_Click(object sender, EventArgs e) { //序列化 Book bk = new Book() { BookID = "12111", PublishDate = DateTime.Parse("2012-2-1 22:12:11"), Price = 433.12M }; p1.InnerText ="序列化,输出时是json格式的字符串:"+ JsonConvert.SerializeObject(bk); //把一个实例,序列化成json语句,输出时是字符串 //Console.WriteLine(JsonConvert.SerializeObject(bk)); //Console.ReadLine(); } } }
aspx前台页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.JsonTest.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="反序列化" /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="序列化" /> <p id="p1" runat="server"></p> </div> </form> </body> </html>
显示结果
JsonConvert.SerializeObject 序例化

JsonConvert.DeserializeObject<Book> 反序例化

浙公网安备 33010602011771号