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

Newtonsoft.Json的地址:

官网:http://json.codeplex.com/

源码地址:https://github.com/JamesNK/Newtonsoft.Json

Newtonsoft.Json.dll下载:https://github.com/JamesNK/Newtonsoft.Json/releases

使用语言:C#

环境:.net Framework 4.6.1 (当前使用)

直接上代码,不多BB:

string 转 json

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

json 转 集合(list)

//转换
var jArray = JsonConvert.DeserializeObject<List<User>>(myStr);
  •  

json 转 对象

//转换
var jArray = JsonConvert.DeserializeObject<User>(myStr)
  •  

完整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
    {
        /// <summary>
        /// 主程序入口
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //创建一个绝对路径
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            WritingJson(desktopPath);
            ReadingJson(desktopPath);

        }
        #region Json转换


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

        }

        /// <summary>
        ///     添加json
        /// </summary>
        /// <param name="desktopPath"></param>
        private static void WritingJson(string desktopPath)
        {
            //创建用户集合
            List<User> users = new List<User>();
            //将添加内容
            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读写


        /// <summary>
        ///     IO读取本地json
        /// </summary>
        /// <param name="desktopPath"></param>
        /// <returns></returns>
        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);
            }
        }

        /// <summary>
        ///     将我们的json保存到本地
        /// </summary>
        /// <param name="desktopPath"></param>
        /// <param name="json"></param>
        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
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值