在.NET使用Newtonsoft.Json轉換,讀取,寫入json
原文出處:http://blog.sina.com.cn/s/blog_70686f3a0101kemg.html
首先,大家要明白什麼是json,瞭解更多關於json方面資料大家可以點選https://www.ibm.com/developerworks/cn/web/wa-lo-json/ ,我在這裡簡單介紹下json:
JSON 即 JavaScript Object Natation,它是一種輕量級的資料交換格式,非常適合於伺服器與 JavaScript 的互動。和 XML 一樣,JSON 也是基於純文字的資料格式。由於 JSON 天生是為 JavaScript 準備的,因此,JSON 的資料格式非常簡單,您可以用 JSON 傳輸一個簡單的 String,Number,Boolean,也可以傳輸一個陣列,或者一個複雜的
Object 物件。
在.NET環境下面,我們使用Json.net來實現JSON資料的序列化和反序列化。
首先點選連線http://sourceforge.net/projects/csjson/?source=dlp下載JSON
.NET外掛和程式碼。
然後在專案中進行引用Newtonsoft.Json.dll
新增名稱空間:using Newtonsoft.Json;
下面介紹json序列化和反序列化的放個重要方法和例子:
JsonConvert.SerializeObject(object value)序列化,它有個過載方法JsonConvert.SerializeObject(object
value, params JsonConverter[] converters)。
JsonConvert.DeserializeObject(string value, Type type),反序列化,它有個過載方法JsonConvert.DeserializeObject(string value, Type type, params JsonConverter[] converters)
這兩個方法可以實現基本的序列化和反序列化要求,請看下面的例子:
JsonConvert.DeserializeObject(string value, Type type),反序列化,它有個過載方法JsonConvert.DeserializeObject(string value, Type type, params JsonConverter[] converters)
這兩個方法可以實現基本的序列化和反序列化要求,請看下面的例子:
首先我們先建一個Person類程式碼如下:
public class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
}
public int Age
{
get { return age; }
set { age = value; }
}
}
1)序列化
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.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
namespace JSONnet
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person person = new Person();
person.Name = "GoldenEasy";
person.Age = 25;
string strSerializeJSON = JsonConvert.SerializeObject(person);
Response.Write(strSerializeJSON);
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person person = new Person();
person.Name = "GoldenEasy";
person.Age = 25;
string strSerializeJSON = JsonConvert.SerializeObject(person);
Response.Write(strSerializeJSON);
}
}
}
}
}
輸出結果:
{"Name":"GoldenEasy","Age":25}
2)反序列化
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.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
namespace JSONnet
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person person = new Person();
person.Name = "GoldenEasy";
person.Age = 25;
string strSerializeJSON = JsonConvert.SerializeObject(person);
Person user = (Person)JsonConvert.DeserializeObject(strSerializeJSON, typeof(Person));
Response.Write(user.Name);
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person person = new Person();
person.Name = "GoldenEasy";
person.Age = 25;
string strSerializeJSON = JsonConvert.SerializeObject(person);
Person user = (Person)JsonConvert.DeserializeObject(strSerializeJSON, typeof(Person));
Response.Write(user.Name);
}
}
}
}
}
輸出結果為:GoldenEasy
【補充:】
參考:http://www.jb51.net/article/19806.htm
前端可用JSON.parse()函式將字串轉成JavaScript JSON物件,如下圖
![](https://i.iter01.com/images/f4e8ac40c93d528aacadd177823f7fd0de8510d33281d97d19db460c91396d21.png)
【補充:】
參考:http://www.jb51.net/article/19806.htm
前端可用JSON.parse()函式將字串轉成JavaScript JSON物件,如下圖
![](https://i.iter01.com/images/f4e8ac40c93d528aacadd177823f7fd0de8510d33281d97d19db460c91396d21.png)
相關文章
- Newtonsoft.Json的使用JSON
- 如何讀取和寫入JSON檔案JSON
- 分享基於.NET動態編譯&Newtonsoft.Json封裝實現JSON轉換器(JsonConverter)原理及JSON操作技巧編譯JSON封裝
- Newtonsoft.Json的使用整理JSON
- 自寫Json轉換工具JSON
- 在.NET使用JSON作為資料交換格式JSON
- Newtonsoft.Json/Json.NET忽略序列化時的意外錯誤JSON
- mybatis&plus系列------Mysql的JSON欄位的讀取和轉換MyBatisMySqlJSON
- Json.NET實現json的讀取,新增,刪除,修改JSON
- Newtonsoft.Json高階用法JSON
- 尷尬的事情又發生Newtonsoft.Json vs Protobuf.netJSON
- Asp .Net Core 讀取appsettings.json配置檔案APPJSON
- c# net6 讀取appsettings.json方法C#APPJSON
- DotNetCore系列:在ASP.NET Core 3.1中獲取xml格式的介面入參並轉化為jsonNetCoreASP.NETXMLJSON
- .NET 5/.NET Core使用EF Core 5連線MySQL資料庫寫入/讀取資料示例教程MySql資料庫
- json的使用(python寫,c++讀)JSONPythonC++
- Json檔案轉換為Excel檔案!涉及讀檔案,時間戳轉化,寫文件JSONExcel時間戳
- Newtonsoft.Json序列化JSON字串問題JSON字串
- 從 Newtonsoft.Json 遷移到 System.Text.JsonJSON
- Python中CSV讀取和轉換Python
- 讀取.net core配置檔案appsetting.json內容APPJSON
- .NET Core中如何讀取appsetting.json配置檔案APPJSON
- JavaScript 寫入與讀取cookieJavaScriptCookie
- fastjson轉換json時,碰到的那些首字母大小寫轉換的坑!ASTJSON
- C# Newtonsoft.Json 高階用法C#JSON
- 讀取JSON資料JSON
- jsoncpp按寫入順序讀取JSON
- Flink 1.9 實戰:使用 SQL 讀取 Kafka 並寫入 MySQLKafkaMySql
- 使用正則 轉換大小寫
- Newtonsoft.Json高階篇:TypeNameHandling設定JSON
- `newtonsoft.json` 指定某個屬性使用特定的時間格式JSON
- 在 Linux 命令列中轉換大小寫Linux命令列
- CYQ.Data 操作 Json 效能測試:對比 Newtonsoft.JsonJSON
- python檔案建立、讀取和寫入Python
- csv檔案的寫入和讀取
- Python JSON 使用指南:解析和轉換資料PythonJSON
- eval() JSON轉換為物件JSON物件
- 資料集轉換JSONJSON
- 精讀《手寫 JSON Parser》JSON