C#使用Newtonsoft.Json进行json数据存储和转换

string 转 json

//转成json
string json = JsonConvert.SerializeObject(users, Formatting.Indented);
  • 1
  • 2

json 转 集合(list)

//转换
var jArray = JsonConvert.DeserializeObject>(myStr);
  • 1
  • 2

json 转 对象

//转换
var jArray = JsonConvert.DeserializeObject(myStr)
  • 1
  • 2

完整Demo代码

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UseNewtonsoftJson
{
   public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Code { get; set; }
    }
    class Program
    {
        /// 
        /// 主程序入口
        /// 
        /// 
        static void Main(string[] args)
        {
            //创建一个绝对路径
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            WritingJson(desktopPath);
            ReadingJson(desktopPath);

        }
        #region Json转换


        /// 
        ///     读取json
        /// 
        /// 
        private static void ReadingJson(string desktopPath)
        {
            string myStr = null;
            //IO读取
            myStr = GetMyJson(desktopPath);
            //转换
            var jArray = JsonConvert.DeserializeObject>(myStr);
            //进一步的转换我就不写啦

        }

        /// 
        ///     添加json
        /// 
        /// 
        private static void WritingJson(string desktopPath)
        {
            //创建用户集合
            List users = new List();
            //将添加内容
            for (int i = 0; i < 100; i++)
            {
                User user = new User()
                {
                    Id = i + 1,
                    Name = "二等碗",
                    Code = "dao"
                };
                users.Add(user);
            }
            //转成json
            string json = JsonConvert.SerializeObject(users, Formatting.Indented);
            //保存到桌面的文件
            SaveMyJson(desktopPath, json);

        }
        #endregion
        #region IO读写


        /// 
        ///     IO读取本地json
        /// 
        /// 
        /// 
        private static string GetMyJson(string desktopPath)
        {
            using (FileStream fsRead = new FileStream(string.Format("{0}\\wandan.json", desktopPath), FileMode.Open))
            {
                //读取加转换
                int fsLen = (int)fsRead.Length;
                byte[] heByte = new byte[fsLen];
                int r = fsRead.Read(heByte, 0, heByte.Length);
                return System.Text.Encoding.UTF8.GetString(heByte);
            }
        }

        /// 
        ///     将我们的json保存到本地
        /// 
        /// 
        /// 
        private static void SaveMyJson(string desktopPath, string json)
        {
            using (FileStream fs = new FileStream(string.Format("{0}\\wandan.json", desktopPath), FileMode.Create))
            {
                //写入
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(json);
                }

            }
        }
        #endregion
    }
}

你可能感兴趣的:(.net)