Json file creation and parsing in C # using Newtonsoft.Json (Json.NET) of

First, add a reference

using Newtonsoft.Json;

Second, the calling code :

//获取图书列表
List<BookInfo> bookList = GetBookList();

//将图书列表转换成Json          
string bookListJson = JsonConvert.SerializeObject(bookList);

//将Json转换回图书列表
List<BookInfo> books = JsonConvert.DeserializeObject<List<BookInfo>>(bookListJson);

Third, save the file to a local object .json, parsing local files as objects .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);
            }

Renderings:

Fourth, other code:


/// <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; }           //销售价格  

} 

/// <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;
}

 

NOTE: If a field do not want to be serialized Json, you can add [Newtonsoft.Json.JsonIgnore] on the characteristic field.

For example prices above examples do not want to be serialized Json:


[Newtonsoft.Json.JsonIgnore]

public Double Price { set; get; }           //销售价格 

Note: This content from: C # in Newtonsoft.Json (Json.NET) using

                                Newtonsoft.Json using C # serialization data generated Json

                                [C #] using the LitJson to create and read documents Json 

 

 

Guess you like

Origin blog.csdn.net/xiaochenXIHUA/article/details/90601532