C#中使用Newtonsoft.Json(Json.NET)的创建Json文件和解析
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json

·
一、添加引用
using Newtonsoft.Json;
二、调用代码:
//获取图书列表
List<BookInfo> bookList = GetBookList();
//将图书列表转换成Json
string bookListJson = JsonConvert.SerializeObject(bookList);
//将Json转换回图书列表
List<BookInfo> books = JsonConvert.DeserializeObject<List<BookInfo>>(bookListJson);
三、将对象保存为.json文件到本地、解析本地.json文件为对象
//获取图书列表
List<BookInfo> bookList = GetBookList();
//将图书列表转换成Json
string bookListJson = JsonConvert.SerializeObject(bookList);
Console.WriteLine(bookListJson);
writeJsonFile(@"e:\booklist.json", bookListJson);
//将序列化的json字符串内容写入Json文件,并且保存
void writeJsonFile(string path, string jsonConents)
{
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
sw.WriteLine(jsonConents);
}
}
}
//将Json转换回图书列表
string jsonData = GetJsonFile(@"e:\booklist.json");
Console.WriteLine(jsonData);
//获取到本地的Json文件并且解析返回对应的json字符串
string GetJsonFile(string filepath)
{
string json = string.Empty;
using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
{
json = sr.ReadToEnd().ToString();
}
}
return json;
}
//反序列化Json字符串内容为图书对象
List<BookInfo> books = JsonConvert.DeserializeObject<List<BookInfo>>(jsonData);
foreach (var item in books)
{
Console.WriteLine("图书ID="+item.BookId);
Console.WriteLine("图书标题=" + item.Title);
Console.WriteLine("图书类别=" + item.Category);
Console.WriteLine("图书作者" + item.Author);
Console.WriteLine("出版日期=" + item.PublishDate);
Console.WriteLine("销售价格=" + item.Price);
}
效果图:
四、其他代码:
/// <summary>
/// 图书信息实体类
/// </summary>
public class BookInfo
{
public int BookId { set; get; } //图书ID
public string Title { set; get; } //图书名称
public string Category { set; get; } //图书分类
public string Author { set; get; } //图书作者
public DateTime PublishDate { set; get; } //出版时间
public Double Price { set; get; } //销售价格
}
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
/// <summary>
/// 获取图书列表
/// </summary>
/// <returns></returns>
public List<BookInfo> GetBookList()
{
List<BookInfo> bookList = new List<BookInfo>();
BookInfo book1 = new BookInfo()
{
BookId = 1,
Category = "CHILDREN",
Title = "Harry Potter",
Author = "J K. Rowling",
PublishDate = new DateTime(2005, 08, 15),
Price = 29.99
};
bookList.Add(book1);
BookInfo book2 = new BookInfo()
{
BookId = 2,
Category = "WEB",
Title = "Learning XML",
Author = "Erik T. Ray",
PublishDate = new DateTime(2003, 10, 18),
Price = 39.95
};
bookList.Add(book2);
return bookList;
}
补充:如果某个字段不想被Json序列化,则可以在该字段上加上[Newtonsoft.Json.JsonIgnore]特性。
例如上述实例中的价格不想被Json序列化:
[Newtonsoft.Json.JsonIgnore]
public Double Price { set; get; } //销售价格
注意:本内容来自:C#中Newtonsoft.Json(Json.NET)的使用
C# 利用Newtonsoft.Json 序列化生成Json数据
阅读全文
AI总结




适用于现代 C++ 的 JSON。
最近提交(Master分支:5 个月前 )
34665ae6
binary -> binary_t
Signed-off-by: Robert Chisholm <robert.chisholm@sheffield.ac.uk> 7 天前
f3dc4684
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.9 to 3.28.10.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/github/codeql-action/compare/9e8d0789d4a0fa9ceb6b1738f7e269594bdd67f0...b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d)
---
updated-dependencies:
- dependency-name: github/codeql-action
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 14 天前
更多推荐
相关推荐
查看更多
json

适用于现代 C++ 的 JSON。
json

A "json" command for massaging JSON on your Unix command line.
json

JSON implementation for Ruby
热门开源项目
活动日历
查看更多
直播时间 2025-03-13 18:32:35

全栈自研企业级AI平台:Java核心技术×私有化部署实战
直播时间 2025-03-11 18:35:18

从0到1:Go IoT 开发平台的架构演进与生态蓝图
直播时间 2025-03-05 14:35:37

国产工作流引擎 终结「996」开发困局!
直播时间 2025-02-25 14:38:13

免费开源宝藏 ShopXO,电商系统搭建秘籍大公开!
直播时间 2025-02-18 14:31:04

从数据孤岛到数据智能 - 企业级数据管理利器深度解析
所有评论(0)