首页 > 其他分享 >Json Net 简单的文件读取和写入

Json Net 简单的文件读取和写入

时间:2022-12-17 22:35:00浏览次数:60  
标签:Newtonsoft 读取 json System Json jsonObj1 using Net

▲ 读写的文件

XiaoMing.json:

{"Name":"小明","Sex":"男","Age":12}

JsonWriteTest.json:

{
  "StartX": 1.23,
  "StartY": 1.24,
  "EndX": 10,
  "EndY": "YY",
  "Favorites": [
    "吃饭",
    "睡觉"
  ],
  "Remark": null
}

引入 Newtonsoft.Json.dll

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadJsonFile();
            WriteJson();

            Console.ReadKey();
        }

        private static void ReadJsonFile()
        {
            string jFileName = "./XiaoMing.json";
            StreamReader sr = File.OpenText(jFileName);
            JsonTextReader jr = new JsonTextReader(sr);
            JObject jObj = JToken.ReadFrom(jr) as JObject;
            Console.WriteLine($"Name:{jObj["Name"].Value<string>()} \nSex:{jObj.Value<string>("Sex")}  \nAge:{jObj["Age"].Value<int>()}");
        }

        private static void WriteJson()
        {
            string fileName = "./JsonWriteTest.json";

            JObject jsonObj1 = new JObject();
            jsonObj1["StartX"] = 1.23;
            jsonObj1["StartY"] = 1.24;
            jsonObj1["EndX"] = 10;
            jsonObj1["EndY"] = "YY";

            // 创建数组
            JArray array = new JArray();
            array.Add(new JValue("吃饭"));
            array.Add(new JValue("睡觉"));
            jsonObj1.Add("Favorites", array);
            jsonObj1.Add("Remark", null);

            //Console.WriteLine(array.ToString());

            string convertString = Convert.ToString(jsonObj1);//将json装换为string
            File.WriteAllText(fileName, convertString, System.Text.Encoding.UTF8);//将内容写进jon文件中
        }
    }
}




参考:

标签:Newtonsoft,读取,json,System,Json,jsonObj1,using,Net
From: https://www.cnblogs.com/huvjie/p/16989692.html

相关文章

  • Arduino UNO使用esp8266以TCP方式连接onenet云
    1.在onenet云平台上先创建一个TCP类型的产品 <1>打开onenet云平台,选择旧版,点击控制台,选择多协议接入(没有注册的先注册)图1-1 <2>多协议接入中选择TCP透传 图1-2......
  • Graph Neural Network——图神经网络
    本文是跟着李沐老师的论文精度系列进行GNN的学习的,详细链接请见:零基础多图详解图神经网络(GNN/GCN)【论文精读】该论文的标题为《AGentleIntroductiontoGraphNeuralNe......
  • Viper读取配置文件
    写项目时总会需要配置文件,Go语言中可以使用viper来读取配置文件。goget"github.com/spf13/viper"例子,现在有一个conf.yamlname:"helloViper"port:8080mode:"d......
  • 002 读取文本文件
    这里提供四个函数,用于读取文本文件。publicstaticStringtextFileContent(Stringpath,Charsetcharset)1//读取文件全部内容2publicstaticStringtextF......
  • CSharp: Proxy Pattern in donet.core 6.0
     usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceGeovin.Du......
  • .NET Core应用如何通过SSL访问MongoDB?
    大家好,我是Edison。最近有一个ASP.NETCore通过SSL证书访问MongoDB的需求,但是在网上发现资料很少,于是调查了一番,做了如下的笔记,希望对你有用。背景在实际场景中,开发环境......
  • springboot启动读取配置文件过程&自定义配置文件处理器
        最近看到看到spring的配置文件放在了resources/config/application.yal文件内部,第一次见。就想的研究下,springboot启动读取配置文件的过程。1.启动过程org.spring......
  • cpp jsoncpp serialize anddeserialize complete example
    //Model/Book.h#pragmaonce#ifndef__Book_H__#define__Book_H__#include<chrono>#include<ctime>#include<fstream>#include<iostream>#include<jsoncpp/......
  • C# Log4net配置文件 总结
    前言因为项目日志太杂乱而且很大,打开一个就卡死了,何况用户电脑也扛不住日志积累,要做一个日志记录器管理日志。但对里面的配置有一些不熟悉(毕竟都是复制粘贴的),所以记录一......
  • fastjson全局日期序列化设置导致JSONField无效
    问题描述fastjson通过代码指定全局序列化返回时间格式,导致使用JSONField注解标注属性的特殊日期返回格式失效使用版本应用名称版本springboot2.0.0.RELEASE......