C#/수업내용

2020.04.21. 수업내용 - Json 파일 만들고 프로그램에 넣기

dev_sr 2020. 4. 21. 18:23

 

 

1.excel 파일->json파일로 만들기

2.컨버트: https://shancarter.github.io/mr-data-converter/ 

 

Mr. Data Converter

 

shancarter.github.io

http://jsonviewer.stack.hu/

 

Online JSON Viewer

 

jsonviewer.stack.hu

3.NewtonSoft.Json 추가

 

4.바인딩(맵핑)클래스 만들기(~~~data.cs)

5.json문자열->객체(역직렬화)

6.dictionary 에 옮겨담기

 

 

7. App class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_013_2
{
    class App
    {
        public App()
        {
            string path = "./cookie_data.json";
            string json = File.ReadAllText(path);
 
            CharacterData[] arrCharacterDatas= JsonConvert.DeserializeObject<CharacterData[]>(json);
 
            //foreach(CharacterData testData in arrCharacterDatas)
            //{
            //    Console.WriteLine("{0}, {1}, {2}", testData.id,testData.name, testData.grade);
            //}
 
 
            Dictionary<int, CharacterData> dicCharacterDatas = new Dictionary<int, CharacterData>();
 
            foreach(CharacterData data in arrCharacterDatas)
            {
                dicCharacterDatas.Add(data.id, data);
            }
 
            foreach(KeyValuePair<int, CharacterData> pair in dicCharacterDatas)
            {
                Console.WriteLine("{0}, {1}, {2}",pair.Key,pair.Value.name,pair.Value.grade);
            }
        }
    }
}
 
 

 

8. CharacterData class 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_013_2
{
    class CharacterData
    {
        public int id;
        public string name;
        public int grade;
    }
}
 
 

 

9. 결과값