User Tools

Site Tools

prog:csharp:241219-001:index

C# Newtonsoft 讀寫 Json

Local Backup

  • 要先使用 NuGat 安裝 Newtonsoft
    using Newtonsoft.Json;
    using System.IO;
  • 寫json格式和儲存
    public class Info
    {
        public string FileName { get; set; }
        public int ImgWidth { get; set; }
        public int ImgHeight { get; set; }
        public int LocationX { get; set; }
        public int LocationY { get; set; }
    }
    
    private void WriteJson()
    {
        //建立資料
        List<Info> listInfoModel = new List<Info>()
        {
            new ImgInfo() { FileName ="test1", ImgWidth = 12, ImgHeight = 13, LocationX = 100, LocationY = 1234},
            new ImgInfo() { FileName ="test2", ImgWidth = 75, ImgHeight = 135, LocationX = 456, LocationY = 9876}
        };
               
        //儲存檔案
        string Filename = "test.json";
        string jsondata = JsonConvert.SerializeObject(listInfoModel);
        File.WriteAllText(Filename, jsondata);
    }
  • 讀json檔
    private void ReadJson()
        {
            string Filename = "test.json";
            string jsonString = File.ReadAllText(Filename);
            List<Info> test = JsonConvert.DeserializeObject<List<Info>>(jsonString);
            MessageBox.Show(test[0].n_LocationX.ToString());
            MessageBox.Show("OK");
        }

完整程式碼

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Newtonsoft.Json;
    using System.IO;
    
    namespace v2015Mytest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public class ImgInfo
            {
                public string s_FileName { get; set; }
                public int n_ImgWidth { get; set; }
                public int n_ImgHeight { get; set; }
                public int n_LocationX { get; set; }
                public int n_LocationY { get; set; }
            }
    
            
            //寫Json
            private void button1_Click(object sender, EventArgs e)
            {
                List<ImgInfo> listInfoModel = new List<ImgInfo>()
                {
                    new ImgInfo() { s_FileName ="test1", n_ImgWidth = 12, n_ImgHeight = 13, n_LocationX = 100, n_LocationY = 1234},
                    new ImgInfo() { s_FileName ="test2", n_ImgWidth = 75, n_ImgHeight = 135, n_LocationX = 456, n_LocationY = 9876}
                };
                
                //MessageBox.Show(JsonConvert.SerializeObject(listInfoModel));
    
                string Filename = "test.json";
                string jsondata = JsonConvert.SerializeObject(listInfoModel);
                File.WriteAllText(Filename, jsondata);
                MessageBox.Show("OK");
            }
            //讀Json
            private void button2_Click(object sender, EventArgs e)
            {
                string Filename = "test.json";
                string jsonString = File.ReadAllText(Filename);
                List<ImgInfo> test = JsonConvert.DeserializeObject<List<ImgInfo>>(jsonString);
                MessageBox.Show(test[1].n_LocationX.ToString());
                MessageBox.Show("OK");
            }
        }
    }
  • 20 person(s) visited this page until now.

Permalink prog/csharp/241219-001/index.txt · Last modified: 2024/12/19 16:21 by jethro

oeffentlich