ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 03/24 직렬화, JSON, 파일시스템 연습
    C#/수업내용 2021. 3. 24. 18:20
    728x90

     

    Program.cs

    using System;
    
    namespace Study07
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Main");
                new App();
            }
        }
    }
    

     

    App.cs

    using System;
    using System.IO;
    using Newtonsoft.Json;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Study07
    {
        public class App
        {
            //생성자 
            public App()
            {
                Console.WriteLine("App");
    
                string txt = File.ReadAllText("./cookie_data.json");
                Console.WriteLine(txt);
    
                Cookie[] cookies = JsonConvert.DeserializeObject<Cookie[]>(txt);
    
                Dictionary<int, Cookie> dicCookieDatas = new Dictionary<int, Cookie>();
    
                dicCookieDatas = cookies.ToDictionary(x => x.id);
    
                foreach (KeyValuePair<int, Cookie> data in dicCookieDatas)
                {
                    Console.WriteLine("Key : {0}, Value : {1} {2} {3} {4} {5}", data.Key, data.Value.name, data.Value.hp, data.Value.skill_name, data.Value.grade, data.Value.sprite_name);                                
                }
    
    
            }
            
        }
    
    }

     

    Cookie.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study07
    {
        public class Cookie
        {
            public int id;
            public string name;
            public int hp;
            public string skill_name;
            public string grade;
            public string sprite_name;
        }
    }
    

     

    밸런스테이블

    cookie_data.xlsx
    0.01MB

     

    Json파일

    cookie_data.json
    0.00MB

     

     

    1. 테이블 작성(엑셀)
    2. 엑셀 to JSON

        shancarter.github.io/mr-data-converter/

        jsonviewer.stack.hu/

    3. JSON -> .json파일(메모장)
    4. NewtonSoft .NET JSON설치
    5. 매핑클래스 생성
    6. json파일 로드 (읽기)
    7. 역직렬화
    8. 배열 to 사전으로
    9. 키로 사전의 요소 출력

    728x90

    'C# > 수업내용' 카테고리의 다른 글

    03/25 싱글턴 디자인패턴 연습  (0) 2021.03.25
    03/24 수업내용 메모  (0) 2021.03.24
    03/24 event연습  (0) 2021.03.24
    03/24 SortedList, IComparable 연습  (0) 2021.03.24
    03/24 callback 연습  (0) 2021.03.24
Designed by Tistory.